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