ntc_thermistor.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. { },
  51. };
  52. /*
  53. * A compensation table should be sorted by the values of .ohm
  54. * in descending order.
  55. * The following compensation tables are from the specification of Murata NTC
  56. * Thermistors Datasheet
  57. */
  58. static const struct ntc_compensation ncpXXwb473[] = {
  59. { .temp_c = -40, .ohm = 1747920 },
  60. { .temp_c = -35, .ohm = 1245428 },
  61. { .temp_c = -30, .ohm = 898485 },
  62. { .temp_c = -25, .ohm = 655802 },
  63. { .temp_c = -20, .ohm = 483954 },
  64. { .temp_c = -15, .ohm = 360850 },
  65. { .temp_c = -10, .ohm = 271697 },
  66. { .temp_c = -5, .ohm = 206463 },
  67. { .temp_c = 0, .ohm = 158214 },
  68. { .temp_c = 5, .ohm = 122259 },
  69. { .temp_c = 10, .ohm = 95227 },
  70. { .temp_c = 15, .ohm = 74730 },
  71. { .temp_c = 20, .ohm = 59065 },
  72. { .temp_c = 25, .ohm = 47000 },
  73. { .temp_c = 30, .ohm = 37643 },
  74. { .temp_c = 35, .ohm = 30334 },
  75. { .temp_c = 40, .ohm = 24591 },
  76. { .temp_c = 45, .ohm = 20048 },
  77. { .temp_c = 50, .ohm = 16433 },
  78. { .temp_c = 55, .ohm = 13539 },
  79. { .temp_c = 60, .ohm = 11209 },
  80. { .temp_c = 65, .ohm = 9328 },
  81. { .temp_c = 70, .ohm = 7798 },
  82. { .temp_c = 75, .ohm = 6544 },
  83. { .temp_c = 80, .ohm = 5518 },
  84. { .temp_c = 85, .ohm = 4674 },
  85. { .temp_c = 90, .ohm = 3972 },
  86. { .temp_c = 95, .ohm = 3388 },
  87. { .temp_c = 100, .ohm = 2902 },
  88. { .temp_c = 105, .ohm = 2494 },
  89. { .temp_c = 110, .ohm = 2150 },
  90. { .temp_c = 115, .ohm = 1860 },
  91. { .temp_c = 120, .ohm = 1615 },
  92. { .temp_c = 125, .ohm = 1406 },
  93. };
  94. static const struct ntc_compensation ncpXXwl333[] = {
  95. { .temp_c = -40, .ohm = 1610154 },
  96. { .temp_c = -35, .ohm = 1130850 },
  97. { .temp_c = -30, .ohm = 802609 },
  98. { .temp_c = -25, .ohm = 575385 },
  99. { .temp_c = -20, .ohm = 416464 },
  100. { .temp_c = -15, .ohm = 304219 },
  101. { .temp_c = -10, .ohm = 224193 },
  102. { .temp_c = -5, .ohm = 166623 },
  103. { .temp_c = 0, .ohm = 124850 },
  104. { .temp_c = 5, .ohm = 94287 },
  105. { .temp_c = 10, .ohm = 71747 },
  106. { .temp_c = 15, .ohm = 54996 },
  107. { .temp_c = 20, .ohm = 42455 },
  108. { .temp_c = 25, .ohm = 33000 },
  109. { .temp_c = 30, .ohm = 25822 },
  110. { .temp_c = 35, .ohm = 20335 },
  111. { .temp_c = 40, .ohm = 16115 },
  112. { .temp_c = 45, .ohm = 12849 },
  113. { .temp_c = 50, .ohm = 10306 },
  114. { .temp_c = 55, .ohm = 8314 },
  115. { .temp_c = 60, .ohm = 6746 },
  116. { .temp_c = 65, .ohm = 5503 },
  117. { .temp_c = 70, .ohm = 4513 },
  118. { .temp_c = 75, .ohm = 3721 },
  119. { .temp_c = 80, .ohm = 3084 },
  120. { .temp_c = 85, .ohm = 2569 },
  121. { .temp_c = 90, .ohm = 2151 },
  122. { .temp_c = 95, .ohm = 1809 },
  123. { .temp_c = 100, .ohm = 1529 },
  124. { .temp_c = 105, .ohm = 1299 },
  125. { .temp_c = 110, .ohm = 1108 },
  126. { .temp_c = 115, .ohm = 949 },
  127. { .temp_c = 120, .ohm = 817 },
  128. { .temp_c = 125, .ohm = 707 },
  129. };
  130. /*
  131. * The following compensation table is from the specification of EPCOS NTC
  132. * Thermistors Datasheet
  133. */
  134. static const struct ntc_compensation b57330v2103[] = {
  135. { .temp_c = -40, .ohm = 190030 },
  136. { .temp_c = -35, .ohm = 145360 },
  137. { .temp_c = -30, .ohm = 112060 },
  138. { .temp_c = -25, .ohm = 87041 },
  139. { .temp_c = -20, .ohm = 68104 },
  140. { .temp_c = -15, .ohm = 53665 },
  141. { .temp_c = -10, .ohm = 42576 },
  142. { .temp_c = -5, .ohm = 34001 },
  143. { .temp_c = 0, .ohm = 27326 },
  144. { .temp_c = 5, .ohm = 22096 },
  145. { .temp_c = 10, .ohm = 17973 },
  146. { .temp_c = 15, .ohm = 14703 },
  147. { .temp_c = 20, .ohm = 12090 },
  148. { .temp_c = 25, .ohm = 10000 },
  149. { .temp_c = 30, .ohm = 8311 },
  150. { .temp_c = 35, .ohm = 6941 },
  151. { .temp_c = 40, .ohm = 5825 },
  152. { .temp_c = 45, .ohm = 4911 },
  153. { .temp_c = 50, .ohm = 4158 },
  154. { .temp_c = 55, .ohm = 3536 },
  155. { .temp_c = 60, .ohm = 3019 },
  156. { .temp_c = 65, .ohm = 2588 },
  157. { .temp_c = 70, .ohm = 2227 },
  158. { .temp_c = 75, .ohm = 1924 },
  159. { .temp_c = 80, .ohm = 1668 },
  160. { .temp_c = 85, .ohm = 1451 },
  161. { .temp_c = 90, .ohm = 1266 },
  162. { .temp_c = 95, .ohm = 1108 },
  163. { .temp_c = 100, .ohm = 973 },
  164. { .temp_c = 105, .ohm = 857 },
  165. { .temp_c = 110, .ohm = 757 },
  166. { .temp_c = 115, .ohm = 671 },
  167. { .temp_c = 120, .ohm = 596 },
  168. { .temp_c = 125, .ohm = 531 },
  169. };
  170. struct ntc_data {
  171. struct device *hwmon_dev;
  172. struct ntc_thermistor_platform_data *pdata;
  173. const struct ntc_compensation *comp;
  174. struct device *dev;
  175. int n_comp;
  176. char name[PLATFORM_NAME_SIZE];
  177. struct thermal_zone_device *tz;
  178. };
  179. #if defined(CONFIG_OF) && IS_ENABLED(CONFIG_IIO)
  180. static int ntc_adc_iio_read(struct ntc_thermistor_platform_data *pdata)
  181. {
  182. struct iio_channel *channel = pdata->chan;
  183. s64 result;
  184. int val, ret;
  185. ret = iio_read_channel_raw(channel, &val);
  186. if (ret < 0) {
  187. pr_err("read channel() error: %d\n", ret);
  188. return ret;
  189. }
  190. /* unit: mV */
  191. result = pdata->pullup_uv * (s64) val;
  192. result >>= 12;
  193. return (int)result;
  194. }
  195. static const struct of_device_id ntc_match[] = {
  196. { .compatible = "murata,ncp15wb473",
  197. .data = &ntc_thermistor_id[0] },
  198. { .compatible = "murata,ncp18wb473",
  199. .data = &ntc_thermistor_id[1] },
  200. { .compatible = "murata,ncp21wb473",
  201. .data = &ntc_thermistor_id[2] },
  202. { .compatible = "murata,ncp03wb473",
  203. .data = &ntc_thermistor_id[3] },
  204. { .compatible = "murata,ncp15wl333",
  205. .data = &ntc_thermistor_id[4] },
  206. { .compatible = "epcos,b57330v2103",
  207. .data = &ntc_thermistor_id[5]},
  208. /* Usage of vendor name "ntc" is deprecated */
  209. { .compatible = "ntc,ncp15wb473",
  210. .data = &ntc_thermistor_id[0] },
  211. { .compatible = "ntc,ncp18wb473",
  212. .data = &ntc_thermistor_id[1] },
  213. { .compatible = "ntc,ncp21wb473",
  214. .data = &ntc_thermistor_id[2] },
  215. { .compatible = "ntc,ncp03wb473",
  216. .data = &ntc_thermistor_id[3] },
  217. { .compatible = "ntc,ncp15wl333",
  218. .data = &ntc_thermistor_id[4] },
  219. { },
  220. };
  221. MODULE_DEVICE_TABLE(of, ntc_match);
  222. static struct ntc_thermistor_platform_data *
  223. ntc_thermistor_parse_dt(struct platform_device *pdev)
  224. {
  225. struct iio_channel *chan;
  226. struct device_node *np = pdev->dev.of_node;
  227. struct ntc_thermistor_platform_data *pdata;
  228. if (!np)
  229. return NULL;
  230. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  231. if (!pdata)
  232. return ERR_PTR(-ENOMEM);
  233. chan = iio_channel_get(&pdev->dev, NULL);
  234. if (IS_ERR(chan))
  235. return ERR_CAST(chan);
  236. if (of_property_read_u32(np, "pullup-uv", &pdata->pullup_uv))
  237. return ERR_PTR(-ENODEV);
  238. if (of_property_read_u32(np, "pullup-ohm", &pdata->pullup_ohm))
  239. return ERR_PTR(-ENODEV);
  240. if (of_property_read_u32(np, "pulldown-ohm", &pdata->pulldown_ohm))
  241. return ERR_PTR(-ENODEV);
  242. if (of_find_property(np, "connected-positive", NULL))
  243. pdata->connect = NTC_CONNECTED_POSITIVE;
  244. else /* status change should be possible if not always on. */
  245. pdata->connect = NTC_CONNECTED_GROUND;
  246. pdata->chan = chan;
  247. pdata->read_uv = ntc_adc_iio_read;
  248. return pdata;
  249. }
  250. static void ntc_iio_channel_release(struct ntc_thermistor_platform_data *pdata)
  251. {
  252. if (pdata->chan)
  253. iio_channel_release(pdata->chan);
  254. }
  255. #else
  256. static struct ntc_thermistor_platform_data *
  257. ntc_thermistor_parse_dt(struct platform_device *pdev)
  258. {
  259. return NULL;
  260. }
  261. #define ntc_match NULL
  262. static void ntc_iio_channel_release(struct ntc_thermistor_platform_data *pdata)
  263. { }
  264. #endif
  265. static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
  266. {
  267. if (divisor == 0 && dividend == 0)
  268. return 0;
  269. if (divisor == 0)
  270. return UINT_MAX;
  271. return div64_u64(dividend, divisor);
  272. }
  273. static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
  274. {
  275. struct ntc_thermistor_platform_data *pdata = data->pdata;
  276. u64 mv = uv / 1000;
  277. u64 pmv = pdata->pullup_uv / 1000;
  278. u64 n, puo, pdo;
  279. puo = pdata->pullup_ohm;
  280. pdo = pdata->pulldown_ohm;
  281. if (mv == 0) {
  282. if (pdata->connect == NTC_CONNECTED_POSITIVE)
  283. return INT_MAX;
  284. return 0;
  285. }
  286. if (mv >= pmv)
  287. return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
  288. 0 : INT_MAX;
  289. if (pdata->connect == NTC_CONNECTED_POSITIVE && puo == 0)
  290. n = div64_u64_safe(pdo * (pmv - mv), mv);
  291. else if (pdata->connect == NTC_CONNECTED_GROUND && pdo == 0)
  292. n = div64_u64_safe(puo * mv, pmv - mv);
  293. else if (pdata->connect == NTC_CONNECTED_POSITIVE)
  294. n = div64_u64_safe(pdo * puo * (pmv - mv),
  295. puo * mv - pdo * (pmv - mv));
  296. else
  297. n = div64_u64_safe(pdo * puo * mv, pdo * (pmv - mv) - puo * mv);
  298. if (n > INT_MAX)
  299. n = INT_MAX;
  300. return n;
  301. }
  302. static void lookup_comp(struct ntc_data *data, unsigned int ohm,
  303. int *i_low, int *i_high)
  304. {
  305. int start, end, mid;
  306. /*
  307. * Handle special cases: Resistance is higher than or equal to
  308. * resistance in first table entry, or resistance is lower or equal
  309. * to resistance in last table entry.
  310. * In these cases, return i_low == i_high, either pointing to the
  311. * beginning or to the end of the table depending on the condition.
  312. */
  313. if (ohm >= data->comp[0].ohm) {
  314. *i_low = 0;
  315. *i_high = 0;
  316. return;
  317. }
  318. if (ohm <= data->comp[data->n_comp - 1].ohm) {
  319. *i_low = data->n_comp - 1;
  320. *i_high = data->n_comp - 1;
  321. return;
  322. }
  323. /* Do a binary search on compensation table */
  324. start = 0;
  325. end = data->n_comp;
  326. while (start < end) {
  327. mid = start + (end - start) / 2;
  328. /*
  329. * start <= mid < end
  330. * data->comp[start].ohm > ohm >= data->comp[end].ohm
  331. *
  332. * We could check for "ohm == data->comp[mid].ohm" here, but
  333. * that is a quite unlikely condition, and we would have to
  334. * check again after updating start. Check it at the end instead
  335. * for simplicity.
  336. */
  337. if (ohm >= data->comp[mid].ohm) {
  338. end = mid;
  339. } else {
  340. start = mid + 1;
  341. /*
  342. * ohm >= data->comp[start].ohm might be true here,
  343. * since we set start to mid + 1. In that case, we are
  344. * done. We could keep going, but the condition is quite
  345. * likely to occur, so it is worth checking for it.
  346. */
  347. if (ohm >= data->comp[start].ohm)
  348. end = start;
  349. }
  350. /*
  351. * start <= end
  352. * data->comp[start].ohm >= ohm >= data->comp[end].ohm
  353. */
  354. }
  355. /*
  356. * start == end
  357. * ohm >= data->comp[end].ohm
  358. */
  359. *i_low = end;
  360. if (ohm == data->comp[end].ohm)
  361. *i_high = end;
  362. else
  363. *i_high = end - 1;
  364. }
  365. static int get_temp_mc(struct ntc_data *data, unsigned int ohm)
  366. {
  367. int low, high;
  368. int temp;
  369. lookup_comp(data, ohm, &low, &high);
  370. if (low == high) {
  371. /* Unable to use linear approximation */
  372. temp = data->comp[low].temp_c * 1000;
  373. } else {
  374. temp = data->comp[low].temp_c * 1000 +
  375. ((data->comp[high].temp_c - data->comp[low].temp_c) *
  376. 1000 * ((int)ohm - (int)data->comp[low].ohm)) /
  377. ((int)data->comp[high].ohm - (int)data->comp[low].ohm);
  378. }
  379. return temp;
  380. }
  381. static int ntc_thermistor_get_ohm(struct ntc_data *data)
  382. {
  383. int read_uv;
  384. if (data->pdata->read_ohm)
  385. return data->pdata->read_ohm();
  386. if (data->pdata->read_uv) {
  387. read_uv = data->pdata->read_uv(data->pdata);
  388. if (read_uv < 0)
  389. return read_uv;
  390. return get_ohm_of_thermistor(data, read_uv);
  391. }
  392. return -EINVAL;
  393. }
  394. static int ntc_read_temp(void *dev, long *temp)
  395. {
  396. struct ntc_data *data = dev_get_drvdata(dev);
  397. int ohm;
  398. ohm = ntc_thermistor_get_ohm(data);
  399. if (ohm < 0)
  400. return ohm;
  401. *temp = get_temp_mc(data, ohm);
  402. return 0;
  403. }
  404. static ssize_t ntc_show_name(struct device *dev,
  405. struct device_attribute *attr, char *buf)
  406. {
  407. struct ntc_data *data = dev_get_drvdata(dev);
  408. return sprintf(buf, "%s\n", data->name);
  409. }
  410. static ssize_t ntc_show_type(struct device *dev,
  411. struct device_attribute *attr, char *buf)
  412. {
  413. return sprintf(buf, "4\n");
  414. }
  415. static ssize_t ntc_show_temp(struct device *dev,
  416. struct device_attribute *attr, char *buf)
  417. {
  418. struct ntc_data *data = dev_get_drvdata(dev);
  419. int ohm;
  420. ohm = ntc_thermistor_get_ohm(data);
  421. if (ohm < 0)
  422. return ohm;
  423. return sprintf(buf, "%d\n", get_temp_mc(data, ohm));
  424. }
  425. static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, ntc_show_type, NULL, 0);
  426. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ntc_show_temp, NULL, 0);
  427. static DEVICE_ATTR(name, S_IRUGO, ntc_show_name, NULL);
  428. static struct attribute *ntc_attributes[] = {
  429. &dev_attr_name.attr,
  430. &sensor_dev_attr_temp1_type.dev_attr.attr,
  431. &sensor_dev_attr_temp1_input.dev_attr.attr,
  432. NULL,
  433. };
  434. static const struct attribute_group ntc_attr_group = {
  435. .attrs = ntc_attributes,
  436. };
  437. static const struct thermal_zone_of_device_ops ntc_of_thermal_ops = {
  438. .get_temp = ntc_read_temp,
  439. };
  440. static int ntc_thermistor_probe(struct platform_device *pdev)
  441. {
  442. const struct of_device_id *of_id =
  443. of_match_device(of_match_ptr(ntc_match), &pdev->dev);
  444. const struct platform_device_id *pdev_id;
  445. struct ntc_thermistor_platform_data *pdata;
  446. struct ntc_data *data;
  447. int ret;
  448. pdata = ntc_thermistor_parse_dt(pdev);
  449. if (IS_ERR(pdata))
  450. return PTR_ERR(pdata);
  451. else if (pdata == NULL)
  452. pdata = dev_get_platdata(&pdev->dev);
  453. if (!pdata) {
  454. dev_err(&pdev->dev, "No platform init data supplied.\n");
  455. return -ENODEV;
  456. }
  457. /* Either one of the two is required. */
  458. if (!pdata->read_uv && !pdata->read_ohm) {
  459. dev_err(&pdev->dev,
  460. "Both read_uv and read_ohm missing. Need either one of the two.\n");
  461. return -EINVAL;
  462. }
  463. if (pdata->read_uv && pdata->read_ohm) {
  464. dev_warn(&pdev->dev,
  465. "Only one of read_uv and read_ohm is needed; ignoring read_uv.\n");
  466. pdata->read_uv = NULL;
  467. }
  468. if (pdata->read_uv && (pdata->pullup_uv == 0 ||
  469. (pdata->pullup_ohm == 0 && pdata->connect ==
  470. NTC_CONNECTED_GROUND) ||
  471. (pdata->pulldown_ohm == 0 && pdata->connect ==
  472. NTC_CONNECTED_POSITIVE) ||
  473. (pdata->connect != NTC_CONNECTED_POSITIVE &&
  474. pdata->connect != NTC_CONNECTED_GROUND))) {
  475. dev_err(&pdev->dev,
  476. "Required data to use read_uv not supplied.\n");
  477. return -EINVAL;
  478. }
  479. data = devm_kzalloc(&pdev->dev, sizeof(struct ntc_data), GFP_KERNEL);
  480. if (!data)
  481. return -ENOMEM;
  482. pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
  483. data->dev = &pdev->dev;
  484. data->pdata = pdata;
  485. strlcpy(data->name, pdev_id->name, sizeof(data->name));
  486. switch (pdev_id->driver_data) {
  487. case TYPE_NCPXXWB473:
  488. data->comp = ncpXXwb473;
  489. data->n_comp = ARRAY_SIZE(ncpXXwb473);
  490. break;
  491. case TYPE_NCPXXWL333:
  492. data->comp = ncpXXwl333;
  493. data->n_comp = ARRAY_SIZE(ncpXXwl333);
  494. break;
  495. case TYPE_B57330V2103:
  496. data->comp = b57330v2103;
  497. data->n_comp = ARRAY_SIZE(b57330v2103);
  498. break;
  499. default:
  500. dev_err(&pdev->dev, "Unknown device type: %lu(%s)\n",
  501. pdev_id->driver_data, pdev_id->name);
  502. return -EINVAL;
  503. }
  504. platform_set_drvdata(pdev, data);
  505. ret = sysfs_create_group(&data->dev->kobj, &ntc_attr_group);
  506. if (ret) {
  507. dev_err(data->dev, "unable to create sysfs files\n");
  508. return ret;
  509. }
  510. data->hwmon_dev = hwmon_device_register(data->dev);
  511. if (IS_ERR(data->hwmon_dev)) {
  512. dev_err(data->dev, "unable to register as hwmon device.\n");
  513. ret = PTR_ERR(data->hwmon_dev);
  514. goto err_after_sysfs;
  515. }
  516. dev_info(&pdev->dev, "Thermistor type: %s successfully probed.\n",
  517. pdev_id->name);
  518. data->tz = thermal_zone_of_sensor_register(data->dev, 0, data->dev,
  519. &ntc_of_thermal_ops);
  520. if (IS_ERR(data->tz)) {
  521. dev_dbg(&pdev->dev, "Failed to register to thermal fw.\n");
  522. data->tz = NULL;
  523. }
  524. return 0;
  525. err_after_sysfs:
  526. sysfs_remove_group(&data->dev->kobj, &ntc_attr_group);
  527. ntc_iio_channel_release(pdata);
  528. return ret;
  529. }
  530. static int ntc_thermistor_remove(struct platform_device *pdev)
  531. {
  532. struct ntc_data *data = platform_get_drvdata(pdev);
  533. struct ntc_thermistor_platform_data *pdata = data->pdata;
  534. hwmon_device_unregister(data->hwmon_dev);
  535. sysfs_remove_group(&data->dev->kobj, &ntc_attr_group);
  536. ntc_iio_channel_release(pdata);
  537. thermal_zone_of_sensor_unregister(data->dev, data->tz);
  538. return 0;
  539. }
  540. static struct platform_driver ntc_thermistor_driver = {
  541. .driver = {
  542. .name = "ntc-thermistor",
  543. .of_match_table = of_match_ptr(ntc_match),
  544. },
  545. .probe = ntc_thermistor_probe,
  546. .remove = ntc_thermistor_remove,
  547. .id_table = ntc_thermistor_id,
  548. };
  549. module_platform_driver(ntc_thermistor_driver);
  550. MODULE_DESCRIPTION("NTC Thermistor Driver");
  551. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  552. MODULE_LICENSE("GPL");
  553. MODULE_ALIAS("platform:ntc-thermistor");