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