ntc_thermistor.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. enum iio_chan_type type;
  227. struct device_node *np = pdev->dev.of_node;
  228. struct ntc_thermistor_platform_data *pdata;
  229. int ret;
  230. if (!np)
  231. return NULL;
  232. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  233. if (!pdata)
  234. return ERR_PTR(-ENOMEM);
  235. chan = iio_channel_get(&pdev->dev, NULL);
  236. if (IS_ERR(chan))
  237. return ERR_CAST(chan);
  238. ret = iio_get_channel_type(chan, &type);
  239. if (ret < 0)
  240. return ERR_PTR(ret);
  241. if (type != IIO_VOLTAGE)
  242. return ERR_PTR(-EINVAL);
  243. if (of_property_read_u32(np, "pullup-uv", &pdata->pullup_uv))
  244. return ERR_PTR(-ENODEV);
  245. if (of_property_read_u32(np, "pullup-ohm", &pdata->pullup_ohm))
  246. return ERR_PTR(-ENODEV);
  247. if (of_property_read_u32(np, "pulldown-ohm", &pdata->pulldown_ohm))
  248. return ERR_PTR(-ENODEV);
  249. if (of_find_property(np, "connected-positive", NULL))
  250. pdata->connect = NTC_CONNECTED_POSITIVE;
  251. else /* status change should be possible if not always on. */
  252. pdata->connect = NTC_CONNECTED_GROUND;
  253. pdata->chan = chan;
  254. pdata->read_uv = ntc_adc_iio_read;
  255. return pdata;
  256. }
  257. static void ntc_iio_channel_release(struct ntc_thermistor_platform_data *pdata)
  258. {
  259. if (pdata->chan)
  260. iio_channel_release(pdata->chan);
  261. }
  262. #else
  263. static struct ntc_thermistor_platform_data *
  264. ntc_thermistor_parse_dt(struct platform_device *pdev)
  265. {
  266. return NULL;
  267. }
  268. #define ntc_match NULL
  269. static void ntc_iio_channel_release(struct ntc_thermistor_platform_data *pdata)
  270. { }
  271. #endif
  272. static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
  273. {
  274. if (divisor == 0 && dividend == 0)
  275. return 0;
  276. if (divisor == 0)
  277. return UINT_MAX;
  278. return div64_u64(dividend, divisor);
  279. }
  280. static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
  281. {
  282. struct ntc_thermistor_platform_data *pdata = data->pdata;
  283. u64 mv = uv / 1000;
  284. u64 pmv = pdata->pullup_uv / 1000;
  285. u64 n, puo, pdo;
  286. puo = pdata->pullup_ohm;
  287. pdo = pdata->pulldown_ohm;
  288. if (mv == 0) {
  289. if (pdata->connect == NTC_CONNECTED_POSITIVE)
  290. return INT_MAX;
  291. return 0;
  292. }
  293. if (mv >= pmv)
  294. return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
  295. 0 : INT_MAX;
  296. if (pdata->connect == NTC_CONNECTED_POSITIVE && puo == 0)
  297. n = div64_u64_safe(pdo * (pmv - mv), mv);
  298. else if (pdata->connect == NTC_CONNECTED_GROUND && pdo == 0)
  299. n = div64_u64_safe(puo * mv, pmv - mv);
  300. else if (pdata->connect == NTC_CONNECTED_POSITIVE)
  301. n = div64_u64_safe(pdo * puo * (pmv - mv),
  302. puo * mv - pdo * (pmv - mv));
  303. else
  304. n = div64_u64_safe(pdo * puo * mv, pdo * (pmv - mv) - puo * mv);
  305. if (n > INT_MAX)
  306. n = INT_MAX;
  307. return n;
  308. }
  309. static void lookup_comp(struct ntc_data *data, unsigned int ohm,
  310. int *i_low, int *i_high)
  311. {
  312. int start, end, mid;
  313. /*
  314. * Handle special cases: Resistance is higher than or equal to
  315. * resistance in first table entry, or resistance is lower or equal
  316. * to resistance in last table entry.
  317. * In these cases, return i_low == i_high, either pointing to the
  318. * beginning or to the end of the table depending on the condition.
  319. */
  320. if (ohm >= data->comp[0].ohm) {
  321. *i_low = 0;
  322. *i_high = 0;
  323. return;
  324. }
  325. if (ohm <= data->comp[data->n_comp - 1].ohm) {
  326. *i_low = data->n_comp - 1;
  327. *i_high = data->n_comp - 1;
  328. return;
  329. }
  330. /* Do a binary search on compensation table */
  331. start = 0;
  332. end = data->n_comp;
  333. while (start < end) {
  334. mid = start + (end - start) / 2;
  335. /*
  336. * start <= mid < end
  337. * data->comp[start].ohm > ohm >= data->comp[end].ohm
  338. *
  339. * We could check for "ohm == data->comp[mid].ohm" here, but
  340. * that is a quite unlikely condition, and we would have to
  341. * check again after updating start. Check it at the end instead
  342. * for simplicity.
  343. */
  344. if (ohm >= data->comp[mid].ohm) {
  345. end = mid;
  346. } else {
  347. start = mid + 1;
  348. /*
  349. * ohm >= data->comp[start].ohm might be true here,
  350. * since we set start to mid + 1. In that case, we are
  351. * done. We could keep going, but the condition is quite
  352. * likely to occur, so it is worth checking for it.
  353. */
  354. if (ohm >= data->comp[start].ohm)
  355. end = start;
  356. }
  357. /*
  358. * start <= end
  359. * data->comp[start].ohm >= ohm >= data->comp[end].ohm
  360. */
  361. }
  362. /*
  363. * start == end
  364. * ohm >= data->comp[end].ohm
  365. */
  366. *i_low = end;
  367. if (ohm == data->comp[end].ohm)
  368. *i_high = end;
  369. else
  370. *i_high = end - 1;
  371. }
  372. static int get_temp_mc(struct ntc_data *data, unsigned int ohm)
  373. {
  374. int low, high;
  375. int temp;
  376. lookup_comp(data, ohm, &low, &high);
  377. if (low == high) {
  378. /* Unable to use linear approximation */
  379. temp = data->comp[low].temp_c * 1000;
  380. } else {
  381. temp = data->comp[low].temp_c * 1000 +
  382. ((data->comp[high].temp_c - data->comp[low].temp_c) *
  383. 1000 * ((int)ohm - (int)data->comp[low].ohm)) /
  384. ((int)data->comp[high].ohm - (int)data->comp[low].ohm);
  385. }
  386. return temp;
  387. }
  388. static int ntc_thermistor_get_ohm(struct ntc_data *data)
  389. {
  390. int read_uv;
  391. if (data->pdata->read_ohm)
  392. return data->pdata->read_ohm();
  393. if (data->pdata->read_uv) {
  394. read_uv = data->pdata->read_uv(data->pdata);
  395. if (read_uv < 0)
  396. return read_uv;
  397. return get_ohm_of_thermistor(data, read_uv);
  398. }
  399. return -EINVAL;
  400. }
  401. static int ntc_read_temp(void *dev, long *temp)
  402. {
  403. struct ntc_data *data = dev_get_drvdata(dev);
  404. int ohm;
  405. ohm = ntc_thermistor_get_ohm(data);
  406. if (ohm < 0)
  407. return ohm;
  408. *temp = get_temp_mc(data, ohm);
  409. return 0;
  410. }
  411. static ssize_t ntc_show_name(struct device *dev,
  412. struct device_attribute *attr, char *buf)
  413. {
  414. struct ntc_data *data = dev_get_drvdata(dev);
  415. return sprintf(buf, "%s\n", data->name);
  416. }
  417. static ssize_t ntc_show_type(struct device *dev,
  418. struct device_attribute *attr, char *buf)
  419. {
  420. return sprintf(buf, "4\n");
  421. }
  422. static ssize_t ntc_show_temp(struct device *dev,
  423. struct device_attribute *attr, char *buf)
  424. {
  425. struct ntc_data *data = dev_get_drvdata(dev);
  426. int ohm;
  427. ohm = ntc_thermistor_get_ohm(data);
  428. if (ohm < 0)
  429. return ohm;
  430. return sprintf(buf, "%d\n", get_temp_mc(data, ohm));
  431. }
  432. static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, ntc_show_type, NULL, 0);
  433. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ntc_show_temp, NULL, 0);
  434. static DEVICE_ATTR(name, S_IRUGO, ntc_show_name, NULL);
  435. static struct attribute *ntc_attributes[] = {
  436. &dev_attr_name.attr,
  437. &sensor_dev_attr_temp1_type.dev_attr.attr,
  438. &sensor_dev_attr_temp1_input.dev_attr.attr,
  439. NULL,
  440. };
  441. static const struct attribute_group ntc_attr_group = {
  442. .attrs = ntc_attributes,
  443. };
  444. static int ntc_thermistor_probe(struct platform_device *pdev)
  445. {
  446. const struct of_device_id *of_id =
  447. of_match_device(of_match_ptr(ntc_match), &pdev->dev);
  448. const struct platform_device_id *pdev_id;
  449. struct ntc_thermistor_platform_data *pdata;
  450. struct ntc_data *data;
  451. int ret;
  452. pdata = ntc_thermistor_parse_dt(pdev);
  453. if (IS_ERR(pdata))
  454. return PTR_ERR(pdata);
  455. else if (pdata == NULL)
  456. pdata = dev_get_platdata(&pdev->dev);
  457. if (!pdata) {
  458. dev_err(&pdev->dev, "No platform init data supplied.\n");
  459. return -ENODEV;
  460. }
  461. /* Either one of the two is required. */
  462. if (!pdata->read_uv && !pdata->read_ohm) {
  463. dev_err(&pdev->dev,
  464. "Both read_uv and read_ohm missing. Need either one of the two.\n");
  465. return -EINVAL;
  466. }
  467. if (pdata->read_uv && pdata->read_ohm) {
  468. dev_warn(&pdev->dev,
  469. "Only one of read_uv and read_ohm is needed; ignoring read_uv.\n");
  470. pdata->read_uv = NULL;
  471. }
  472. if (pdata->read_uv && (pdata->pullup_uv == 0 ||
  473. (pdata->pullup_ohm == 0 && pdata->connect ==
  474. NTC_CONNECTED_GROUND) ||
  475. (pdata->pulldown_ohm == 0 && pdata->connect ==
  476. NTC_CONNECTED_POSITIVE) ||
  477. (pdata->connect != NTC_CONNECTED_POSITIVE &&
  478. pdata->connect != NTC_CONNECTED_GROUND))) {
  479. dev_err(&pdev->dev,
  480. "Required data to use read_uv not supplied.\n");
  481. return -EINVAL;
  482. }
  483. data = devm_kzalloc(&pdev->dev, sizeof(struct ntc_data), GFP_KERNEL);
  484. if (!data)
  485. return -ENOMEM;
  486. pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
  487. data->dev = &pdev->dev;
  488. data->pdata = pdata;
  489. strlcpy(data->name, pdev_id->name, sizeof(data->name));
  490. switch (pdev_id->driver_data) {
  491. case TYPE_NCPXXWB473:
  492. data->comp = ncpXXwb473;
  493. data->n_comp = ARRAY_SIZE(ncpXXwb473);
  494. break;
  495. case TYPE_NCPXXWL333:
  496. data->comp = ncpXXwl333;
  497. data->n_comp = ARRAY_SIZE(ncpXXwl333);
  498. break;
  499. case TYPE_B57330V2103:
  500. data->comp = b57330v2103;
  501. data->n_comp = ARRAY_SIZE(b57330v2103);
  502. break;
  503. default:
  504. dev_err(&pdev->dev, "Unknown device type: %lu(%s)\n",
  505. pdev_id->driver_data, pdev_id->name);
  506. return -EINVAL;
  507. }
  508. platform_set_drvdata(pdev, data);
  509. ret = sysfs_create_group(&data->dev->kobj, &ntc_attr_group);
  510. if (ret) {
  511. dev_err(data->dev, "unable to create sysfs files\n");
  512. return ret;
  513. }
  514. data->hwmon_dev = hwmon_device_register(data->dev);
  515. if (IS_ERR(data->hwmon_dev)) {
  516. dev_err(data->dev, "unable to register as hwmon device.\n");
  517. ret = PTR_ERR(data->hwmon_dev);
  518. goto err_after_sysfs;
  519. }
  520. dev_info(&pdev->dev, "Thermistor type: %s successfully probed.\n",
  521. pdev_id->name);
  522. data->tz = thermal_zone_of_sensor_register(data->dev, 0, data->dev,
  523. ntc_read_temp, NULL);
  524. if (IS_ERR(data->tz)) {
  525. dev_dbg(&pdev->dev, "Failed to register to thermal fw.\n");
  526. data->tz = NULL;
  527. }
  528. return 0;
  529. err_after_sysfs:
  530. sysfs_remove_group(&data->dev->kobj, &ntc_attr_group);
  531. ntc_iio_channel_release(pdata);
  532. return ret;
  533. }
  534. static int ntc_thermistor_remove(struct platform_device *pdev)
  535. {
  536. struct ntc_data *data = platform_get_drvdata(pdev);
  537. struct ntc_thermistor_platform_data *pdata = data->pdata;
  538. hwmon_device_unregister(data->hwmon_dev);
  539. sysfs_remove_group(&data->dev->kobj, &ntc_attr_group);
  540. ntc_iio_channel_release(pdata);
  541. thermal_zone_of_sensor_unregister(data->dev, data->tz);
  542. return 0;
  543. }
  544. static struct platform_driver ntc_thermistor_driver = {
  545. .driver = {
  546. .name = "ntc-thermistor",
  547. .owner = THIS_MODULE,
  548. .of_match_table = of_match_ptr(ntc_match),
  549. },
  550. .probe = ntc_thermistor_probe,
  551. .remove = ntc_thermistor_remove,
  552. .id_table = ntc_thermistor_id,
  553. };
  554. module_platform_driver(ntc_thermistor_driver);
  555. MODULE_DESCRIPTION("NTC Thermistor Driver");
  556. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  557. MODULE_LICENSE("GPL");
  558. MODULE_ALIAS("platform:ntc-thermistor");