Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System |
| 4 | * driver |
| 5 | * |
| 6 | * Copyright 2019 Analog Devices Inc. |
| 7 | */ |
| 8 | #include <linux/bitfield.h> |
| 9 | #include <linux/completion.h> |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/iio/iio.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/of_gpio.h> |
| 17 | #include <linux/regmap.h> |
| 18 | #include <linux/spi/spi.h> |
| 19 | |
| 20 | /* register map */ |
| 21 | #define LTC2983_STATUS_REG 0x0000 |
| 22 | #define LTC2983_TEMP_RES_START_REG 0x0010 |
| 23 | #define LTC2983_TEMP_RES_END_REG 0x005F |
| 24 | #define LTC2983_GLOBAL_CONFIG_REG 0x00F0 |
| 25 | #define LTC2983_MULT_CHANNEL_START_REG 0x00F4 |
| 26 | #define LTC2983_MULT_CHANNEL_END_REG 0x00F7 |
| 27 | #define LTC2983_MUX_CONFIG_REG 0x00FF |
| 28 | #define LTC2983_CHAN_ASSIGN_START_REG 0x0200 |
| 29 | #define LTC2983_CHAN_ASSIGN_END_REG 0x024F |
| 30 | #define LTC2983_CUST_SENS_TBL_START_REG 0x0250 |
| 31 | #define LTC2983_CUST_SENS_TBL_END_REG 0x03CF |
| 32 | |
| 33 | #define LTC2983_DIFFERENTIAL_CHAN_MIN 2 |
| 34 | #define LTC2983_MAX_CHANNELS_NR 20 |
| 35 | #define LTC2983_MIN_CHANNELS_NR 1 |
| 36 | #define LTC2983_SLEEP 0x97 |
| 37 | #define LTC2983_CUSTOM_STEINHART_SIZE 24 |
| 38 | #define LTC2983_CUSTOM_SENSOR_ENTRY_SZ 6 |
| 39 | #define LTC2983_CUSTOM_STEINHART_ENTRY_SZ 4 |
| 40 | |
| 41 | #define LTC2983_CHAN_START_ADDR(chan) \ |
| 42 | (((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG) |
| 43 | #define LTC2983_CHAN_RES_ADDR(chan) \ |
| 44 | (((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG) |
| 45 | #define LTC2983_THERMOCOUPLE_DIFF_MASK BIT(3) |
| 46 | #define LTC2983_THERMOCOUPLE_SGL(x) \ |
| 47 | FIELD_PREP(LTC2983_THERMOCOUPLE_DIFF_MASK, x) |
| 48 | #define LTC2983_THERMOCOUPLE_OC_CURR_MASK GENMASK(1, 0) |
| 49 | #define LTC2983_THERMOCOUPLE_OC_CURR(x) \ |
| 50 | FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CURR_MASK, x) |
| 51 | #define LTC2983_THERMOCOUPLE_OC_CHECK_MASK BIT(2) |
| 52 | #define LTC2983_THERMOCOUPLE_OC_CHECK(x) \ |
| 53 | FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CHECK_MASK, x) |
| 54 | |
| 55 | #define LTC2983_THERMISTOR_DIFF_MASK BIT(2) |
| 56 | #define LTC2983_THERMISTOR_SGL(x) \ |
| 57 | FIELD_PREP(LTC2983_THERMISTOR_DIFF_MASK, x) |
| 58 | #define LTC2983_THERMISTOR_R_SHARE_MASK BIT(1) |
| 59 | #define LTC2983_THERMISTOR_R_SHARE(x) \ |
| 60 | FIELD_PREP(LTC2983_THERMISTOR_R_SHARE_MASK, x) |
| 61 | #define LTC2983_THERMISTOR_C_ROTATE_MASK BIT(0) |
| 62 | #define LTC2983_THERMISTOR_C_ROTATE(x) \ |
| 63 | FIELD_PREP(LTC2983_THERMISTOR_C_ROTATE_MASK, x) |
| 64 | |
| 65 | #define LTC2983_DIODE_DIFF_MASK BIT(2) |
| 66 | #define LTC2983_DIODE_SGL(x) \ |
| 67 | FIELD_PREP(LTC2983_DIODE_DIFF_MASK, x) |
| 68 | #define LTC2983_DIODE_3_CONV_CYCLE_MASK BIT(1) |
| 69 | #define LTC2983_DIODE_3_CONV_CYCLE(x) \ |
| 70 | FIELD_PREP(LTC2983_DIODE_3_CONV_CYCLE_MASK, x) |
| 71 | #define LTC2983_DIODE_AVERAGE_ON_MASK BIT(0) |
| 72 | #define LTC2983_DIODE_AVERAGE_ON(x) \ |
| 73 | FIELD_PREP(LTC2983_DIODE_AVERAGE_ON_MASK, x) |
| 74 | |
| 75 | #define LTC2983_RTD_4_WIRE_MASK BIT(3) |
| 76 | #define LTC2983_RTD_ROTATION_MASK BIT(1) |
| 77 | #define LTC2983_RTD_C_ROTATE(x) \ |
| 78 | FIELD_PREP(LTC2983_RTD_ROTATION_MASK, x) |
| 79 | #define LTC2983_RTD_KELVIN_R_SENSE_MASK GENMASK(3, 2) |
| 80 | #define LTC2983_RTD_N_WIRES_MASK GENMASK(3, 2) |
| 81 | #define LTC2983_RTD_N_WIRES(x) \ |
| 82 | FIELD_PREP(LTC2983_RTD_N_WIRES_MASK, x) |
| 83 | #define LTC2983_RTD_R_SHARE_MASK BIT(0) |
| 84 | #define LTC2983_RTD_R_SHARE(x) \ |
| 85 | FIELD_PREP(LTC2983_RTD_R_SHARE_MASK, 1) |
| 86 | |
| 87 | #define LTC2983_COMMON_HARD_FAULT_MASK GENMASK(31, 30) |
| 88 | #define LTC2983_COMMON_SOFT_FAULT_MASK GENMASK(27, 25) |
| 89 | |
| 90 | #define LTC2983_STATUS_START_MASK BIT(7) |
| 91 | #define LTC2983_STATUS_START(x) FIELD_PREP(LTC2983_STATUS_START_MASK, x) |
Nuno Sá | b76d26d | 2021-08-11 15:32:20 +0200 | [diff] [blame] | 92 | #define LTC2983_STATUS_UP_MASK GENMASK(7, 6) |
| 93 | #define LTC2983_STATUS_UP(reg) FIELD_GET(LTC2983_STATUS_UP_MASK, reg) |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 94 | |
| 95 | #define LTC2983_STATUS_CHAN_SEL_MASK GENMASK(4, 0) |
| 96 | #define LTC2983_STATUS_CHAN_SEL(x) \ |
| 97 | FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x) |
| 98 | |
| 99 | #define LTC2983_TEMP_UNITS_MASK BIT(2) |
| 100 | #define LTC2983_TEMP_UNITS(x) FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x) |
| 101 | |
| 102 | #define LTC2983_NOTCH_FREQ_MASK GENMASK(1, 0) |
| 103 | #define LTC2983_NOTCH_FREQ(x) FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x) |
| 104 | |
| 105 | #define LTC2983_RES_VALID_MASK BIT(24) |
| 106 | #define LTC2983_DATA_MASK GENMASK(23, 0) |
| 107 | #define LTC2983_DATA_SIGN_BIT 23 |
| 108 | |
| 109 | #define LTC2983_CHAN_TYPE_MASK GENMASK(31, 27) |
| 110 | #define LTC2983_CHAN_TYPE(x) FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x) |
| 111 | |
| 112 | /* cold junction for thermocouples and rsense for rtd's and thermistor's */ |
| 113 | #define LTC2983_CHAN_ASSIGN_MASK GENMASK(26, 22) |
| 114 | #define LTC2983_CHAN_ASSIGN(x) FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x) |
| 115 | |
| 116 | #define LTC2983_CUSTOM_LEN_MASK GENMASK(5, 0) |
| 117 | #define LTC2983_CUSTOM_LEN(x) FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x) |
| 118 | |
| 119 | #define LTC2983_CUSTOM_ADDR_MASK GENMASK(11, 6) |
| 120 | #define LTC2983_CUSTOM_ADDR(x) FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x) |
| 121 | |
| 122 | #define LTC2983_THERMOCOUPLE_CFG_MASK GENMASK(21, 18) |
| 123 | #define LTC2983_THERMOCOUPLE_CFG(x) \ |
| 124 | FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x) |
| 125 | #define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK GENMASK(31, 29) |
| 126 | #define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK GENMASK(28, 25) |
| 127 | |
| 128 | #define LTC2983_RTD_CFG_MASK GENMASK(21, 18) |
| 129 | #define LTC2983_RTD_CFG(x) FIELD_PREP(LTC2983_RTD_CFG_MASK, x) |
| 130 | #define LTC2983_RTD_EXC_CURRENT_MASK GENMASK(17, 14) |
| 131 | #define LTC2983_RTD_EXC_CURRENT(x) \ |
| 132 | FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x) |
| 133 | #define LTC2983_RTD_CURVE_MASK GENMASK(13, 12) |
| 134 | #define LTC2983_RTD_CURVE(x) FIELD_PREP(LTC2983_RTD_CURVE_MASK, x) |
| 135 | |
| 136 | #define LTC2983_THERMISTOR_CFG_MASK GENMASK(21, 19) |
| 137 | #define LTC2983_THERMISTOR_CFG(x) \ |
| 138 | FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x) |
| 139 | #define LTC2983_THERMISTOR_EXC_CURRENT_MASK GENMASK(18, 15) |
| 140 | #define LTC2983_THERMISTOR_EXC_CURRENT(x) \ |
| 141 | FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x) |
| 142 | |
| 143 | #define LTC2983_DIODE_CFG_MASK GENMASK(26, 24) |
| 144 | #define LTC2983_DIODE_CFG(x) FIELD_PREP(LTC2983_DIODE_CFG_MASK, x) |
| 145 | #define LTC2983_DIODE_EXC_CURRENT_MASK GENMASK(23, 22) |
| 146 | #define LTC2983_DIODE_EXC_CURRENT(x) \ |
| 147 | FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x) |
| 148 | #define LTC2983_DIODE_IDEAL_FACTOR_MASK GENMASK(21, 0) |
| 149 | #define LTC2983_DIODE_IDEAL_FACTOR(x) \ |
| 150 | FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x) |
| 151 | |
| 152 | #define LTC2983_R_SENSE_VAL_MASK GENMASK(26, 0) |
| 153 | #define LTC2983_R_SENSE_VAL(x) FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x) |
| 154 | |
| 155 | #define LTC2983_ADC_SINGLE_ENDED_MASK BIT(26) |
| 156 | #define LTC2983_ADC_SINGLE_ENDED(x) \ |
| 157 | FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x) |
| 158 | |
| 159 | enum { |
| 160 | LTC2983_SENSOR_THERMOCOUPLE = 1, |
| 161 | LTC2983_SENSOR_THERMOCOUPLE_CUSTOM = 9, |
| 162 | LTC2983_SENSOR_RTD = 10, |
| 163 | LTC2983_SENSOR_RTD_CUSTOM = 18, |
| 164 | LTC2983_SENSOR_THERMISTOR = 19, |
| 165 | LTC2983_SENSOR_THERMISTOR_STEINHART = 26, |
| 166 | LTC2983_SENSOR_THERMISTOR_CUSTOM = 27, |
| 167 | LTC2983_SENSOR_DIODE = 28, |
| 168 | LTC2983_SENSOR_SENSE_RESISTOR = 29, |
| 169 | LTC2983_SENSOR_DIRECT_ADC = 30, |
| 170 | }; |
| 171 | |
| 172 | #define to_thermocouple(_sensor) \ |
| 173 | container_of(_sensor, struct ltc2983_thermocouple, sensor) |
| 174 | |
| 175 | #define to_rtd(_sensor) \ |
| 176 | container_of(_sensor, struct ltc2983_rtd, sensor) |
| 177 | |
| 178 | #define to_thermistor(_sensor) \ |
| 179 | container_of(_sensor, struct ltc2983_thermistor, sensor) |
| 180 | |
| 181 | #define to_diode(_sensor) \ |
| 182 | container_of(_sensor, struct ltc2983_diode, sensor) |
| 183 | |
| 184 | #define to_rsense(_sensor) \ |
| 185 | container_of(_sensor, struct ltc2983_rsense, sensor) |
| 186 | |
| 187 | #define to_adc(_sensor) \ |
| 188 | container_of(_sensor, struct ltc2983_adc, sensor) |
| 189 | |
| 190 | struct ltc2983_data { |
| 191 | struct regmap *regmap; |
| 192 | struct spi_device *spi; |
| 193 | struct mutex lock; |
| 194 | struct completion completion; |
| 195 | struct iio_chan_spec *iio_chan; |
| 196 | struct ltc2983_sensor **sensors; |
| 197 | u32 mux_delay_config; |
| 198 | u32 filter_notch_freq; |
| 199 | u16 custom_table_size; |
| 200 | u8 num_channels; |
| 201 | u8 iio_channels; |
| 202 | /* |
| 203 | * DMA (thus cache coherency maintenance) requires the |
| 204 | * transfer buffers to live in their own cache lines. |
| 205 | * Holds the converted temperature |
| 206 | */ |
| 207 | __be32 temp ____cacheline_aligned; |
| 208 | }; |
| 209 | |
| 210 | struct ltc2983_sensor { |
| 211 | int (*fault_handler)(const struct ltc2983_data *st, const u32 result); |
| 212 | int (*assign_chan)(struct ltc2983_data *st, |
| 213 | const struct ltc2983_sensor *sensor); |
| 214 | /* specifies the sensor channel */ |
| 215 | u32 chan; |
| 216 | /* sensor type */ |
| 217 | u32 type; |
| 218 | }; |
| 219 | |
| 220 | struct ltc2983_custom_sensor { |
| 221 | /* raw table sensor data */ |
| 222 | u8 *table; |
| 223 | size_t size; |
| 224 | /* address offset */ |
| 225 | s8 offset; |
| 226 | bool is_steinhart; |
| 227 | }; |
| 228 | |
| 229 | struct ltc2983_thermocouple { |
| 230 | struct ltc2983_sensor sensor; |
| 231 | struct ltc2983_custom_sensor *custom; |
| 232 | u32 sensor_config; |
| 233 | u32 cold_junction_chan; |
| 234 | }; |
| 235 | |
| 236 | struct ltc2983_rtd { |
| 237 | struct ltc2983_sensor sensor; |
| 238 | struct ltc2983_custom_sensor *custom; |
| 239 | u32 sensor_config; |
| 240 | u32 r_sense_chan; |
| 241 | u32 excitation_current; |
| 242 | u32 rtd_curve; |
| 243 | }; |
| 244 | |
| 245 | struct ltc2983_thermistor { |
| 246 | struct ltc2983_sensor sensor; |
| 247 | struct ltc2983_custom_sensor *custom; |
| 248 | u32 sensor_config; |
| 249 | u32 r_sense_chan; |
| 250 | u32 excitation_current; |
| 251 | }; |
| 252 | |
| 253 | struct ltc2983_diode { |
| 254 | struct ltc2983_sensor sensor; |
| 255 | u32 sensor_config; |
| 256 | u32 excitation_current; |
| 257 | u32 ideal_factor_value; |
| 258 | }; |
| 259 | |
| 260 | struct ltc2983_rsense { |
| 261 | struct ltc2983_sensor sensor; |
| 262 | u32 r_sense_val; |
| 263 | }; |
| 264 | |
| 265 | struct ltc2983_adc { |
| 266 | struct ltc2983_sensor sensor; |
| 267 | bool single_ended; |
| 268 | }; |
| 269 | |
| 270 | /* |
| 271 | * Convert to Q format numbers. These number's are integers where |
| 272 | * the number of integer and fractional bits are specified. The resolution |
| 273 | * is given by 1/@resolution and tell us the number of fractional bits. For |
| 274 | * instance a resolution of 2^-10 means we have 10 fractional bits. |
| 275 | */ |
| 276 | static u32 __convert_to_raw(const u64 val, const u32 resolution) |
| 277 | { |
| 278 | u64 __res = val * resolution; |
| 279 | |
| 280 | /* all values are multiplied by 1000000 to remove the fraction */ |
| 281 | do_div(__res, 1000000); |
| 282 | |
| 283 | return __res; |
| 284 | } |
| 285 | |
| 286 | static u32 __convert_to_raw_sign(const u64 val, const u32 resolution) |
| 287 | { |
| 288 | s64 __res = -(s32)val; |
| 289 | |
| 290 | __res = __convert_to_raw(__res, resolution); |
| 291 | |
| 292 | return (u32)-__res; |
| 293 | } |
| 294 | |
| 295 | static int __ltc2983_fault_handler(const struct ltc2983_data *st, |
| 296 | const u32 result, const u32 hard_mask, |
| 297 | const u32 soft_mask) |
| 298 | { |
| 299 | const struct device *dev = &st->spi->dev; |
| 300 | |
| 301 | if (result & hard_mask) { |
| 302 | dev_err(dev, "Invalid conversion: Sensor HARD fault\n"); |
| 303 | return -EIO; |
| 304 | } else if (result & soft_mask) { |
| 305 | /* just print a warning */ |
| 306 | dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n"); |
| 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int __ltc2983_chan_assign_common(const struct ltc2983_data *st, |
| 313 | const struct ltc2983_sensor *sensor, |
| 314 | u32 chan_val) |
| 315 | { |
| 316 | u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan); |
| 317 | __be32 __chan_val; |
| 318 | |
| 319 | chan_val |= LTC2983_CHAN_TYPE(sensor->type); |
| 320 | dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg, |
| 321 | chan_val); |
| 322 | __chan_val = cpu_to_be32(chan_val); |
| 323 | return regmap_bulk_write(st->regmap, reg, &__chan_val, |
| 324 | sizeof(__chan_val)); |
| 325 | } |
| 326 | |
| 327 | static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st, |
| 328 | struct ltc2983_custom_sensor *custom, |
| 329 | u32 *chan_val) |
| 330 | { |
| 331 | u32 reg; |
| 332 | u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ : |
| 333 | LTC2983_CUSTOM_SENSOR_ENTRY_SZ; |
| 334 | const struct device *dev = &st->spi->dev; |
| 335 | /* |
| 336 | * custom->size holds the raw size of the table. However, when |
| 337 | * configuring the sensor channel, we must write the number of |
| 338 | * entries of the table minus 1. For steinhart sensors 0 is written |
| 339 | * since the size is constant! |
| 340 | */ |
| 341 | const u8 len = custom->is_steinhart ? 0 : |
| 342 | (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1; |
| 343 | /* |
| 344 | * Check if the offset was assigned already. It should be for steinhart |
| 345 | * sensors. When coming from sleep, it should be assigned for all. |
| 346 | */ |
| 347 | if (custom->offset < 0) { |
| 348 | /* |
| 349 | * This needs to be done again here because, from the moment |
| 350 | * when this test was done (successfully) for this custom |
| 351 | * sensor, a steinhart sensor might have been added changing |
| 352 | * custom_table_size... |
| 353 | */ |
| 354 | if (st->custom_table_size + custom->size > |
| 355 | (LTC2983_CUST_SENS_TBL_END_REG - |
| 356 | LTC2983_CUST_SENS_TBL_START_REG) + 1) { |
| 357 | dev_err(dev, |
| 358 | "Not space left(%d) for new custom sensor(%zu)", |
| 359 | st->custom_table_size, |
| 360 | custom->size); |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | |
| 364 | custom->offset = st->custom_table_size / |
| 365 | LTC2983_CUSTOM_SENSOR_ENTRY_SZ; |
| 366 | st->custom_table_size += custom->size; |
| 367 | } |
| 368 | |
| 369 | reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG; |
| 370 | |
| 371 | *chan_val |= LTC2983_CUSTOM_LEN(len); |
| 372 | *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset); |
| 373 | dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu", |
| 374 | reg, custom->offset, |
| 375 | custom->size); |
| 376 | /* write custom sensor table */ |
| 377 | return regmap_bulk_write(st->regmap, reg, custom->table, custom->size); |
| 378 | } |
| 379 | |
| 380 | static struct ltc2983_custom_sensor *__ltc2983_custom_sensor_new( |
| 381 | struct ltc2983_data *st, |
| 382 | const struct device_node *np, |
| 383 | const char *propname, |
| 384 | const bool is_steinhart, |
| 385 | const u32 resolution, |
| 386 | const bool has_signed) |
| 387 | { |
| 388 | struct ltc2983_custom_sensor *new_custom; |
| 389 | u8 index, n_entries, tbl = 0; |
| 390 | struct device *dev = &st->spi->dev; |
| 391 | /* |
| 392 | * For custom steinhart, the full u32 is taken. For all the others |
| 393 | * the MSB is discarded. |
| 394 | */ |
Rohit Sarkar | 5a464c6 | 2020-03-28 12:23:04 +0530 | [diff] [blame] | 395 | const u8 n_size = is_steinhart ? 4 : 3; |
| 396 | const u8 e_size = is_steinhart ? sizeof(u32) : sizeof(u64); |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 397 | |
| 398 | n_entries = of_property_count_elems_of_size(np, propname, e_size); |
| 399 | /* n_entries must be an even number */ |
| 400 | if (!n_entries || (n_entries % 2) != 0) { |
| 401 | dev_err(dev, "Number of entries either 0 or not even\n"); |
| 402 | return ERR_PTR(-EINVAL); |
| 403 | } |
| 404 | |
| 405 | new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL); |
| 406 | if (!new_custom) |
| 407 | return ERR_PTR(-ENOMEM); |
| 408 | |
| 409 | new_custom->size = n_entries * n_size; |
| 410 | /* check Steinhart size */ |
| 411 | if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE) { |
| 412 | dev_err(dev, "Steinhart sensors size(%zu) must be 24", |
| 413 | new_custom->size); |
| 414 | return ERR_PTR(-EINVAL); |
| 415 | } |
| 416 | /* Check space on the table. */ |
| 417 | if (st->custom_table_size + new_custom->size > |
| 418 | (LTC2983_CUST_SENS_TBL_END_REG - |
| 419 | LTC2983_CUST_SENS_TBL_START_REG) + 1) { |
| 420 | dev_err(dev, "No space left(%d) for new custom sensor(%zu)", |
| 421 | st->custom_table_size, new_custom->size); |
| 422 | return ERR_PTR(-EINVAL); |
| 423 | } |
| 424 | |
| 425 | /* allocate the table */ |
| 426 | new_custom->table = devm_kzalloc(dev, new_custom->size, GFP_KERNEL); |
| 427 | if (!new_custom->table) |
| 428 | return ERR_PTR(-ENOMEM); |
| 429 | |
| 430 | for (index = 0; index < n_entries; index++) { |
| 431 | u64 temp = 0, j; |
| 432 | /* |
| 433 | * Steinhart sensors are configured with raw values in the |
| 434 | * devicetree. For the other sensors we must convert the |
| 435 | * value to raw. The odd index's correspond to temperarures |
| 436 | * and always have 1/1024 of resolution. Temperatures also |
| 437 | * come in kelvin, so signed values is not possible |
| 438 | */ |
| 439 | if (!is_steinhart) { |
| 440 | of_property_read_u64_index(np, propname, index, &temp); |
| 441 | |
| 442 | if ((index % 2) != 0) |
| 443 | temp = __convert_to_raw(temp, 1024); |
| 444 | else if (has_signed && (s64)temp < 0) |
| 445 | temp = __convert_to_raw_sign(temp, resolution); |
| 446 | else |
| 447 | temp = __convert_to_raw(temp, resolution); |
| 448 | } else { |
Colin Ian King | 2e19b6c | 2019-11-05 20:28:18 +0000 | [diff] [blame] | 449 | u32 t32; |
| 450 | |
| 451 | of_property_read_u32_index(np, propname, index, &t32); |
| 452 | temp = t32; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | for (j = 0; j < n_size; j++) |
| 456 | new_custom->table[tbl++] = |
| 457 | temp >> (8 * (n_size - j - 1)); |
| 458 | } |
| 459 | |
| 460 | new_custom->is_steinhart = is_steinhart; |
| 461 | /* |
| 462 | * This is done to first add all the steinhart sensors to the table, |
| 463 | * in order to maximize the table usage. If we mix adding steinhart |
| 464 | * with the other sensors, we might have to do some roundup to make |
| 465 | * sure that sensor_addr - 0x250(start address) is a multiple of 4 |
| 466 | * (for steinhart), and a multiple of 6 for all the other sensors. |
| 467 | * Since we have const 24 bytes for steinhart sensors and 24 is |
| 468 | * also a multiple of 6, we guarantee that the first non-steinhart |
| 469 | * sensor will sit in a correct address without the need of filling |
| 470 | * addresses. |
| 471 | */ |
| 472 | if (is_steinhart) { |
| 473 | new_custom->offset = st->custom_table_size / |
| 474 | LTC2983_CUSTOM_STEINHART_ENTRY_SZ; |
| 475 | st->custom_table_size += new_custom->size; |
| 476 | } else { |
| 477 | /* mark as unset. This is checked later on the assign phase */ |
| 478 | new_custom->offset = -1; |
| 479 | } |
| 480 | |
| 481 | return new_custom; |
| 482 | } |
| 483 | |
| 484 | static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st, |
| 485 | const u32 result) |
| 486 | { |
| 487 | return __ltc2983_fault_handler(st, result, |
| 488 | LTC2983_THERMOCOUPLE_HARD_FAULT_MASK, |
| 489 | LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK); |
| 490 | } |
| 491 | |
| 492 | static int ltc2983_common_fault_handler(const struct ltc2983_data *st, |
| 493 | const u32 result) |
| 494 | { |
| 495 | return __ltc2983_fault_handler(st, result, |
| 496 | LTC2983_COMMON_HARD_FAULT_MASK, |
| 497 | LTC2983_COMMON_SOFT_FAULT_MASK); |
| 498 | } |
| 499 | |
| 500 | static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st, |
| 501 | const struct ltc2983_sensor *sensor) |
| 502 | { |
| 503 | struct ltc2983_thermocouple *thermo = to_thermocouple(sensor); |
| 504 | u32 chan_val; |
| 505 | |
| 506 | chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan); |
| 507 | chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config); |
| 508 | |
| 509 | if (thermo->custom) { |
| 510 | int ret; |
| 511 | |
| 512 | ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom, |
| 513 | &chan_val); |
| 514 | if (ret) |
| 515 | return ret; |
| 516 | } |
| 517 | return __ltc2983_chan_assign_common(st, sensor, chan_val); |
| 518 | } |
| 519 | |
| 520 | static int ltc2983_rtd_assign_chan(struct ltc2983_data *st, |
| 521 | const struct ltc2983_sensor *sensor) |
| 522 | { |
| 523 | struct ltc2983_rtd *rtd = to_rtd(sensor); |
| 524 | u32 chan_val; |
| 525 | |
| 526 | chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan); |
| 527 | chan_val |= LTC2983_RTD_CFG(rtd->sensor_config); |
| 528 | chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current); |
| 529 | chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve); |
| 530 | |
| 531 | if (rtd->custom) { |
| 532 | int ret; |
| 533 | |
| 534 | ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom, |
| 535 | &chan_val); |
| 536 | if (ret) |
| 537 | return ret; |
| 538 | } |
| 539 | return __ltc2983_chan_assign_common(st, sensor, chan_val); |
| 540 | } |
| 541 | |
| 542 | static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st, |
| 543 | const struct ltc2983_sensor *sensor) |
| 544 | { |
| 545 | struct ltc2983_thermistor *thermistor = to_thermistor(sensor); |
| 546 | u32 chan_val; |
| 547 | |
| 548 | chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan); |
| 549 | chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config); |
| 550 | chan_val |= |
| 551 | LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current); |
| 552 | |
| 553 | if (thermistor->custom) { |
| 554 | int ret; |
| 555 | |
| 556 | ret = __ltc2983_chan_custom_sensor_assign(st, |
| 557 | thermistor->custom, |
| 558 | &chan_val); |
| 559 | if (ret) |
| 560 | return ret; |
| 561 | } |
| 562 | return __ltc2983_chan_assign_common(st, sensor, chan_val); |
| 563 | } |
| 564 | |
| 565 | static int ltc2983_diode_assign_chan(struct ltc2983_data *st, |
| 566 | const struct ltc2983_sensor *sensor) |
| 567 | { |
| 568 | struct ltc2983_diode *diode = to_diode(sensor); |
| 569 | u32 chan_val; |
| 570 | |
| 571 | chan_val = LTC2983_DIODE_CFG(diode->sensor_config); |
| 572 | chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current); |
| 573 | chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value); |
| 574 | |
| 575 | return __ltc2983_chan_assign_common(st, sensor, chan_val); |
| 576 | } |
| 577 | |
| 578 | static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st, |
| 579 | const struct ltc2983_sensor *sensor) |
| 580 | { |
| 581 | struct ltc2983_rsense *rsense = to_rsense(sensor); |
| 582 | u32 chan_val; |
| 583 | |
| 584 | chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val); |
| 585 | |
| 586 | return __ltc2983_chan_assign_common(st, sensor, chan_val); |
| 587 | } |
| 588 | |
| 589 | static int ltc2983_adc_assign_chan(struct ltc2983_data *st, |
| 590 | const struct ltc2983_sensor *sensor) |
| 591 | { |
| 592 | struct ltc2983_adc *adc = to_adc(sensor); |
| 593 | u32 chan_val; |
| 594 | |
| 595 | chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended); |
| 596 | |
| 597 | return __ltc2983_chan_assign_common(st, sensor, chan_val); |
| 598 | } |
| 599 | |
| 600 | static struct ltc2983_sensor *ltc2983_thermocouple_new( |
| 601 | const struct device_node *child, |
| 602 | struct ltc2983_data *st, |
| 603 | const struct ltc2983_sensor *sensor) |
| 604 | { |
| 605 | struct ltc2983_thermocouple *thermo; |
| 606 | struct device_node *phandle; |
| 607 | u32 oc_current; |
| 608 | int ret; |
| 609 | |
| 610 | thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL); |
| 611 | if (!thermo) |
| 612 | return ERR_PTR(-ENOMEM); |
| 613 | |
| 614 | if (of_property_read_bool(child, "adi,single-ended")) |
| 615 | thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1); |
| 616 | |
| 617 | ret = of_property_read_u32(child, "adi,sensor-oc-current-microamp", |
| 618 | &oc_current); |
| 619 | if (!ret) { |
| 620 | switch (oc_current) { |
| 621 | case 10: |
| 622 | thermo->sensor_config |= |
| 623 | LTC2983_THERMOCOUPLE_OC_CURR(0); |
| 624 | break; |
| 625 | case 100: |
| 626 | thermo->sensor_config |= |
| 627 | LTC2983_THERMOCOUPLE_OC_CURR(1); |
| 628 | break; |
| 629 | case 500: |
| 630 | thermo->sensor_config |= |
| 631 | LTC2983_THERMOCOUPLE_OC_CURR(2); |
| 632 | break; |
| 633 | case 1000: |
| 634 | thermo->sensor_config |= |
| 635 | LTC2983_THERMOCOUPLE_OC_CURR(3); |
| 636 | break; |
| 637 | default: |
| 638 | dev_err(&st->spi->dev, |
| 639 | "Invalid open circuit current:%u", oc_current); |
| 640 | return ERR_PTR(-EINVAL); |
| 641 | } |
| 642 | |
| 643 | thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1); |
| 644 | } |
| 645 | /* validate channel index */ |
| 646 | if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) && |
| 647 | sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { |
| 648 | dev_err(&st->spi->dev, |
| 649 | "Invalid chann:%d for differential thermocouple", |
| 650 | sensor->chan); |
| 651 | return ERR_PTR(-EINVAL); |
| 652 | } |
| 653 | |
| 654 | phandle = of_parse_phandle(child, "adi,cold-junction-handle", 0); |
| 655 | if (phandle) { |
| 656 | int ret; |
| 657 | |
| 658 | ret = of_property_read_u32(phandle, "reg", |
| 659 | &thermo->cold_junction_chan); |
| 660 | if (ret) { |
| 661 | /* |
| 662 | * This would be catched later but we can just return |
| 663 | * the error right away. |
| 664 | */ |
| 665 | dev_err(&st->spi->dev, "Property reg must be given\n"); |
| 666 | of_node_put(phandle); |
| 667 | return ERR_PTR(-EINVAL); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | /* check custom sensor */ |
| 672 | if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) { |
| 673 | const char *propname = "adi,custom-thermocouple"; |
| 674 | |
| 675 | thermo->custom = __ltc2983_custom_sensor_new(st, child, |
| 676 | propname, false, |
| 677 | 16384, true); |
| 678 | if (IS_ERR(thermo->custom)) { |
| 679 | of_node_put(phandle); |
| 680 | return ERR_CAST(thermo->custom); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | /* set common parameters */ |
| 685 | thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler; |
| 686 | thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan; |
| 687 | |
| 688 | of_node_put(phandle); |
| 689 | return &thermo->sensor; |
| 690 | } |
| 691 | |
| 692 | static struct ltc2983_sensor *ltc2983_rtd_new(const struct device_node *child, |
| 693 | struct ltc2983_data *st, |
| 694 | const struct ltc2983_sensor *sensor) |
| 695 | { |
| 696 | struct ltc2983_rtd *rtd; |
| 697 | int ret = 0; |
| 698 | struct device *dev = &st->spi->dev; |
| 699 | struct device_node *phandle; |
| 700 | u32 excitation_current = 0, n_wires = 0; |
| 701 | |
| 702 | rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL); |
| 703 | if (!rtd) |
| 704 | return ERR_PTR(-ENOMEM); |
| 705 | |
| 706 | phandle = of_parse_phandle(child, "adi,rsense-handle", 0); |
| 707 | if (!phandle) { |
| 708 | dev_err(dev, "Property adi,rsense-handle missing or invalid"); |
| 709 | return ERR_PTR(-EINVAL); |
| 710 | } |
| 711 | |
| 712 | ret = of_property_read_u32(phandle, "reg", &rtd->r_sense_chan); |
| 713 | if (ret) { |
| 714 | dev_err(dev, "Property reg must be given\n"); |
| 715 | goto fail; |
| 716 | } |
| 717 | |
| 718 | ret = of_property_read_u32(child, "adi,number-of-wires", &n_wires); |
| 719 | if (!ret) { |
| 720 | switch (n_wires) { |
| 721 | case 2: |
| 722 | rtd->sensor_config = LTC2983_RTD_N_WIRES(0); |
| 723 | break; |
| 724 | case 3: |
| 725 | rtd->sensor_config = LTC2983_RTD_N_WIRES(1); |
| 726 | break; |
| 727 | case 4: |
| 728 | rtd->sensor_config = LTC2983_RTD_N_WIRES(2); |
| 729 | break; |
| 730 | case 5: |
| 731 | /* 4 wires, Kelvin Rsense */ |
| 732 | rtd->sensor_config = LTC2983_RTD_N_WIRES(3); |
| 733 | break; |
| 734 | default: |
| 735 | dev_err(dev, "Invalid number of wires:%u\n", n_wires); |
| 736 | ret = -EINVAL; |
| 737 | goto fail; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | if (of_property_read_bool(child, "adi,rsense-share")) { |
| 742 | /* Current rotation is only available with rsense sharing */ |
| 743 | if (of_property_read_bool(child, "adi,current-rotate")) { |
| 744 | if (n_wires == 2 || n_wires == 3) { |
| 745 | dev_err(dev, |
| 746 | "Rotation not allowed for 2/3 Wire RTDs"); |
| 747 | ret = -EINVAL; |
| 748 | goto fail; |
| 749 | } |
| 750 | rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1); |
| 751 | } else { |
| 752 | rtd->sensor_config |= LTC2983_RTD_R_SHARE(1); |
| 753 | } |
| 754 | } |
| 755 | /* |
| 756 | * rtd channel indexes are a bit more complicated to validate. |
| 757 | * For 4wire RTD with rotation, the channel selection cannot be |
| 758 | * >=19 since the chann + 1 is used in this configuration. |
| 759 | * For 4wire RTDs with kelvin rsense, the rsense channel cannot be |
| 760 | * <=1 since chanel - 1 and channel - 2 are used. |
| 761 | */ |
| 762 | if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) { |
| 763 | /* 4-wire */ |
| 764 | u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN, |
| 765 | max = LTC2983_MAX_CHANNELS_NR; |
| 766 | |
| 767 | if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK) |
| 768 | max = LTC2983_MAX_CHANNELS_NR - 1; |
| 769 | |
| 770 | if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK) |
| 771 | == LTC2983_RTD_KELVIN_R_SENSE_MASK) && |
| 772 | (rtd->r_sense_chan <= min)) { |
| 773 | /* kelvin rsense*/ |
| 774 | dev_err(dev, |
| 775 | "Invalid rsense chann:%d to use in kelvin rsense", |
| 776 | rtd->r_sense_chan); |
| 777 | |
| 778 | ret = -EINVAL; |
| 779 | goto fail; |
| 780 | } |
| 781 | |
| 782 | if (sensor->chan < min || sensor->chan > max) { |
| 783 | dev_err(dev, "Invalid chann:%d for the rtd config", |
| 784 | sensor->chan); |
| 785 | |
| 786 | ret = -EINVAL; |
| 787 | goto fail; |
| 788 | } |
| 789 | } else { |
| 790 | /* same as differential case */ |
| 791 | if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { |
| 792 | dev_err(&st->spi->dev, |
| 793 | "Invalid chann:%d for RTD", sensor->chan); |
| 794 | |
| 795 | ret = -EINVAL; |
| 796 | goto fail; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /* check custom sensor */ |
| 801 | if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) { |
| 802 | rtd->custom = __ltc2983_custom_sensor_new(st, child, |
| 803 | "adi,custom-rtd", |
| 804 | false, 2048, false); |
| 805 | if (IS_ERR(rtd->custom)) { |
| 806 | of_node_put(phandle); |
| 807 | return ERR_CAST(rtd->custom); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /* set common parameters */ |
| 812 | rtd->sensor.fault_handler = ltc2983_common_fault_handler; |
| 813 | rtd->sensor.assign_chan = ltc2983_rtd_assign_chan; |
| 814 | |
| 815 | ret = of_property_read_u32(child, "adi,excitation-current-microamp", |
| 816 | &excitation_current); |
| 817 | if (ret) { |
| 818 | /* default to 5uA */ |
| 819 | rtd->excitation_current = 1; |
| 820 | } else { |
| 821 | switch (excitation_current) { |
| 822 | case 5: |
| 823 | rtd->excitation_current = 0x01; |
| 824 | break; |
| 825 | case 10: |
| 826 | rtd->excitation_current = 0x02; |
| 827 | break; |
| 828 | case 25: |
| 829 | rtd->excitation_current = 0x03; |
| 830 | break; |
| 831 | case 50: |
| 832 | rtd->excitation_current = 0x04; |
| 833 | break; |
| 834 | case 100: |
| 835 | rtd->excitation_current = 0x05; |
| 836 | break; |
| 837 | case 250: |
| 838 | rtd->excitation_current = 0x06; |
| 839 | break; |
| 840 | case 500: |
| 841 | rtd->excitation_current = 0x07; |
| 842 | break; |
| 843 | case 1000: |
| 844 | rtd->excitation_current = 0x08; |
| 845 | break; |
| 846 | default: |
| 847 | dev_err(&st->spi->dev, |
| 848 | "Invalid value for excitation current(%u)", |
| 849 | excitation_current); |
| 850 | ret = -EINVAL; |
| 851 | goto fail; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | of_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve); |
| 856 | |
| 857 | of_node_put(phandle); |
| 858 | return &rtd->sensor; |
| 859 | fail: |
| 860 | of_node_put(phandle); |
| 861 | return ERR_PTR(ret); |
| 862 | } |
| 863 | |
| 864 | static struct ltc2983_sensor *ltc2983_thermistor_new( |
| 865 | const struct device_node *child, |
| 866 | struct ltc2983_data *st, |
| 867 | const struct ltc2983_sensor *sensor) |
| 868 | { |
| 869 | struct ltc2983_thermistor *thermistor; |
| 870 | struct device *dev = &st->spi->dev; |
| 871 | struct device_node *phandle; |
| 872 | u32 excitation_current = 0; |
| 873 | int ret = 0; |
| 874 | |
| 875 | thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL); |
| 876 | if (!thermistor) |
| 877 | return ERR_PTR(-ENOMEM); |
| 878 | |
| 879 | phandle = of_parse_phandle(child, "adi,rsense-handle", 0); |
| 880 | if (!phandle) { |
| 881 | dev_err(dev, "Property adi,rsense-handle missing or invalid"); |
| 882 | return ERR_PTR(-EINVAL); |
| 883 | } |
| 884 | |
| 885 | ret = of_property_read_u32(phandle, "reg", &thermistor->r_sense_chan); |
| 886 | if (ret) { |
| 887 | dev_err(dev, "rsense channel must be configured...\n"); |
| 888 | goto fail; |
| 889 | } |
| 890 | |
| 891 | if (of_property_read_bool(child, "adi,single-ended")) { |
| 892 | thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1); |
| 893 | } else if (of_property_read_bool(child, "adi,rsense-share")) { |
| 894 | /* rotation is only possible if sharing rsense */ |
| 895 | if (of_property_read_bool(child, "adi,current-rotate")) |
| 896 | thermistor->sensor_config = |
| 897 | LTC2983_THERMISTOR_C_ROTATE(1); |
| 898 | else |
| 899 | thermistor->sensor_config = |
| 900 | LTC2983_THERMISTOR_R_SHARE(1); |
| 901 | } |
| 902 | /* validate channel index */ |
| 903 | if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) && |
| 904 | sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { |
| 905 | dev_err(&st->spi->dev, |
| 906 | "Invalid chann:%d for differential thermistor", |
| 907 | sensor->chan); |
| 908 | ret = -EINVAL; |
| 909 | goto fail; |
| 910 | } |
| 911 | |
| 912 | /* check custom sensor */ |
| 913 | if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) { |
| 914 | bool steinhart = false; |
| 915 | const char *propname; |
| 916 | |
| 917 | if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) { |
| 918 | steinhart = true; |
| 919 | propname = "adi,custom-steinhart"; |
| 920 | } else { |
| 921 | propname = "adi,custom-thermistor"; |
| 922 | } |
| 923 | |
| 924 | thermistor->custom = __ltc2983_custom_sensor_new(st, child, |
| 925 | propname, |
| 926 | steinhart, |
| 927 | 64, false); |
| 928 | if (IS_ERR(thermistor->custom)) { |
| 929 | of_node_put(phandle); |
| 930 | return ERR_CAST(thermistor->custom); |
| 931 | } |
| 932 | } |
| 933 | /* set common parameters */ |
| 934 | thermistor->sensor.fault_handler = ltc2983_common_fault_handler; |
| 935 | thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan; |
| 936 | |
| 937 | ret = of_property_read_u32(child, "adi,excitation-current-nanoamp", |
| 938 | &excitation_current); |
| 939 | if (ret) { |
| 940 | /* Auto range is not allowed for custom sensors */ |
| 941 | if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) |
| 942 | /* default to 1uA */ |
| 943 | thermistor->excitation_current = 0x03; |
| 944 | else |
| 945 | /* default to auto-range */ |
| 946 | thermistor->excitation_current = 0x0c; |
| 947 | } else { |
| 948 | switch (excitation_current) { |
| 949 | case 0: |
| 950 | /* auto range */ |
| 951 | if (sensor->type >= |
| 952 | LTC2983_SENSOR_THERMISTOR_STEINHART) { |
| 953 | dev_err(&st->spi->dev, |
| 954 | "Auto Range not allowed for custom sensors\n"); |
| 955 | ret = -EINVAL; |
| 956 | goto fail; |
| 957 | } |
| 958 | thermistor->excitation_current = 0x0c; |
| 959 | break; |
| 960 | case 250: |
| 961 | thermistor->excitation_current = 0x01; |
| 962 | break; |
| 963 | case 500: |
| 964 | thermistor->excitation_current = 0x02; |
| 965 | break; |
| 966 | case 1000: |
| 967 | thermistor->excitation_current = 0x03; |
| 968 | break; |
| 969 | case 5000: |
| 970 | thermistor->excitation_current = 0x04; |
| 971 | break; |
| 972 | case 10000: |
| 973 | thermistor->excitation_current = 0x05; |
| 974 | break; |
| 975 | case 25000: |
| 976 | thermistor->excitation_current = 0x06; |
| 977 | break; |
| 978 | case 50000: |
| 979 | thermistor->excitation_current = 0x07; |
| 980 | break; |
| 981 | case 100000: |
| 982 | thermistor->excitation_current = 0x08; |
| 983 | break; |
| 984 | case 250000: |
| 985 | thermistor->excitation_current = 0x09; |
| 986 | break; |
| 987 | case 500000: |
| 988 | thermistor->excitation_current = 0x0a; |
| 989 | break; |
| 990 | case 1000000: |
| 991 | thermistor->excitation_current = 0x0b; |
| 992 | break; |
| 993 | default: |
| 994 | dev_err(&st->spi->dev, |
| 995 | "Invalid value for excitation current(%u)", |
| 996 | excitation_current); |
| 997 | ret = -EINVAL; |
| 998 | goto fail; |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | of_node_put(phandle); |
| 1003 | return &thermistor->sensor; |
| 1004 | fail: |
| 1005 | of_node_put(phandle); |
| 1006 | return ERR_PTR(ret); |
| 1007 | } |
| 1008 | |
| 1009 | static struct ltc2983_sensor *ltc2983_diode_new( |
| 1010 | const struct device_node *child, |
| 1011 | const struct ltc2983_data *st, |
| 1012 | const struct ltc2983_sensor *sensor) |
| 1013 | { |
| 1014 | struct ltc2983_diode *diode; |
| 1015 | u32 temp = 0, excitation_current = 0; |
| 1016 | int ret; |
| 1017 | |
| 1018 | diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL); |
| 1019 | if (!diode) |
| 1020 | return ERR_PTR(-ENOMEM); |
| 1021 | |
| 1022 | if (of_property_read_bool(child, "adi,single-ended")) |
| 1023 | diode->sensor_config = LTC2983_DIODE_SGL(1); |
| 1024 | |
| 1025 | if (of_property_read_bool(child, "adi,three-conversion-cycles")) |
| 1026 | diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1); |
| 1027 | |
| 1028 | if (of_property_read_bool(child, "adi,average-on")) |
| 1029 | diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1); |
| 1030 | |
| 1031 | /* validate channel index */ |
| 1032 | if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) && |
| 1033 | sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { |
| 1034 | dev_err(&st->spi->dev, |
| 1035 | "Invalid chann:%d for differential thermistor", |
| 1036 | sensor->chan); |
| 1037 | return ERR_PTR(-EINVAL); |
| 1038 | } |
| 1039 | /* set common parameters */ |
| 1040 | diode->sensor.fault_handler = ltc2983_common_fault_handler; |
| 1041 | diode->sensor.assign_chan = ltc2983_diode_assign_chan; |
| 1042 | |
| 1043 | ret = of_property_read_u32(child, "adi,excitation-current-microamp", |
| 1044 | &excitation_current); |
| 1045 | if (!ret) { |
| 1046 | switch (excitation_current) { |
| 1047 | case 10: |
| 1048 | diode->excitation_current = 0x00; |
| 1049 | break; |
| 1050 | case 20: |
| 1051 | diode->excitation_current = 0x01; |
| 1052 | break; |
| 1053 | case 40: |
| 1054 | diode->excitation_current = 0x02; |
| 1055 | break; |
| 1056 | case 80: |
| 1057 | diode->excitation_current = 0x03; |
| 1058 | break; |
| 1059 | default: |
| 1060 | dev_err(&st->spi->dev, |
| 1061 | "Invalid value for excitation current(%u)", |
| 1062 | excitation_current); |
| 1063 | return ERR_PTR(-EINVAL); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | of_property_read_u32(child, "adi,ideal-factor-value", &temp); |
| 1068 | |
| 1069 | /* 2^20 resolution */ |
| 1070 | diode->ideal_factor_value = __convert_to_raw(temp, 1048576); |
| 1071 | |
| 1072 | return &diode->sensor; |
| 1073 | } |
| 1074 | |
| 1075 | static struct ltc2983_sensor *ltc2983_r_sense_new(struct device_node *child, |
| 1076 | struct ltc2983_data *st, |
| 1077 | const struct ltc2983_sensor *sensor) |
| 1078 | { |
| 1079 | struct ltc2983_rsense *rsense; |
| 1080 | int ret; |
| 1081 | u32 temp; |
| 1082 | |
| 1083 | rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL); |
| 1084 | if (!rsense) |
| 1085 | return ERR_PTR(-ENOMEM); |
| 1086 | |
| 1087 | /* validate channel index */ |
| 1088 | if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { |
| 1089 | dev_err(&st->spi->dev, "Invalid chann:%d for r_sense", |
| 1090 | sensor->chan); |
| 1091 | return ERR_PTR(-EINVAL); |
| 1092 | } |
| 1093 | |
| 1094 | ret = of_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp); |
| 1095 | if (ret) { |
| 1096 | dev_err(&st->spi->dev, "Property adi,rsense-val-milli-ohms missing\n"); |
| 1097 | return ERR_PTR(-EINVAL); |
| 1098 | } |
| 1099 | /* |
| 1100 | * Times 1000 because we have milli-ohms and __convert_to_raw |
| 1101 | * expects scales of 1000000 which are used for all other |
| 1102 | * properties. |
| 1103 | * 2^10 resolution |
| 1104 | */ |
| 1105 | rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024); |
| 1106 | |
| 1107 | /* set common parameters */ |
| 1108 | rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan; |
| 1109 | |
| 1110 | return &rsense->sensor; |
| 1111 | } |
| 1112 | |
| 1113 | static struct ltc2983_sensor *ltc2983_adc_new(struct device_node *child, |
| 1114 | struct ltc2983_data *st, |
| 1115 | const struct ltc2983_sensor *sensor) |
| 1116 | { |
| 1117 | struct ltc2983_adc *adc; |
| 1118 | |
| 1119 | adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL); |
| 1120 | if (!adc) |
| 1121 | return ERR_PTR(-ENOMEM); |
| 1122 | |
| 1123 | if (of_property_read_bool(child, "adi,single-ended")) |
| 1124 | adc->single_ended = true; |
| 1125 | |
| 1126 | if (!adc->single_ended && |
| 1127 | sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { |
| 1128 | dev_err(&st->spi->dev, "Invalid chan:%d for differential adc\n", |
| 1129 | sensor->chan); |
| 1130 | return ERR_PTR(-EINVAL); |
| 1131 | } |
| 1132 | /* set common parameters */ |
| 1133 | adc->sensor.assign_chan = ltc2983_adc_assign_chan; |
| 1134 | adc->sensor.fault_handler = ltc2983_common_fault_handler; |
| 1135 | |
| 1136 | return &adc->sensor; |
| 1137 | } |
| 1138 | |
| 1139 | static int ltc2983_chan_read(struct ltc2983_data *st, |
| 1140 | const struct ltc2983_sensor *sensor, int *val) |
| 1141 | { |
| 1142 | u32 start_conversion = 0; |
| 1143 | int ret; |
| 1144 | unsigned long time; |
| 1145 | |
| 1146 | start_conversion = LTC2983_STATUS_START(true); |
| 1147 | start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan); |
| 1148 | dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n", |
| 1149 | sensor->chan, start_conversion); |
| 1150 | /* start conversion */ |
| 1151 | ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion); |
| 1152 | if (ret) |
| 1153 | return ret; |
| 1154 | |
| 1155 | reinit_completion(&st->completion); |
| 1156 | /* |
| 1157 | * wait for conversion to complete. |
| 1158 | * 300 ms should be more than enough to complete the conversion. |
| 1159 | * Depending on the sensor configuration, there are 2/3 conversions |
| 1160 | * cycles of 82ms. |
| 1161 | */ |
| 1162 | time = wait_for_completion_timeout(&st->completion, |
| 1163 | msecs_to_jiffies(300)); |
| 1164 | if (!time) { |
| 1165 | dev_warn(&st->spi->dev, "Conversion timed out\n"); |
| 1166 | return -ETIMEDOUT; |
| 1167 | } |
| 1168 | |
| 1169 | /* read the converted data */ |
| 1170 | ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan), |
| 1171 | &st->temp, sizeof(st->temp)); |
| 1172 | if (ret) |
| 1173 | return ret; |
| 1174 | |
| 1175 | *val = __be32_to_cpu(st->temp); |
| 1176 | |
| 1177 | if (!(LTC2983_RES_VALID_MASK & *val)) { |
| 1178 | dev_err(&st->spi->dev, "Invalid conversion detected\n"); |
| 1179 | return -EIO; |
| 1180 | } |
| 1181 | |
| 1182 | ret = sensor->fault_handler(st, *val); |
| 1183 | if (ret) |
| 1184 | return ret; |
| 1185 | |
| 1186 | *val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT); |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | static int ltc2983_read_raw(struct iio_dev *indio_dev, |
| 1191 | struct iio_chan_spec const *chan, |
| 1192 | int *val, int *val2, long mask) |
| 1193 | { |
| 1194 | struct ltc2983_data *st = iio_priv(indio_dev); |
| 1195 | int ret; |
| 1196 | |
| 1197 | /* sanity check */ |
| 1198 | if (chan->address >= st->num_channels) { |
| 1199 | dev_err(&st->spi->dev, "Invalid chan address:%ld", |
| 1200 | chan->address); |
| 1201 | return -EINVAL; |
| 1202 | } |
| 1203 | |
| 1204 | switch (mask) { |
| 1205 | case IIO_CHAN_INFO_RAW: |
| 1206 | mutex_lock(&st->lock); |
| 1207 | ret = ltc2983_chan_read(st, st->sensors[chan->address], val); |
| 1208 | mutex_unlock(&st->lock); |
| 1209 | return ret ?: IIO_VAL_INT; |
| 1210 | case IIO_CHAN_INFO_SCALE: |
| 1211 | switch (chan->type) { |
| 1212 | case IIO_TEMP: |
| 1213 | /* value in milli degrees */ |
| 1214 | *val = 1000; |
| 1215 | /* 2^10 */ |
| 1216 | *val2 = 1024; |
| 1217 | return IIO_VAL_FRACTIONAL; |
| 1218 | case IIO_VOLTAGE: |
| 1219 | /* value in millivolt */ |
| 1220 | *val = 1000; |
| 1221 | /* 2^21 */ |
| 1222 | *val2 = 2097152; |
| 1223 | return IIO_VAL_FRACTIONAL; |
| 1224 | default: |
| 1225 | return -EINVAL; |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | return -EINVAL; |
| 1230 | } |
| 1231 | |
| 1232 | static int ltc2983_reg_access(struct iio_dev *indio_dev, |
| 1233 | unsigned int reg, |
| 1234 | unsigned int writeval, |
| 1235 | unsigned int *readval) |
| 1236 | { |
| 1237 | struct ltc2983_data *st = iio_priv(indio_dev); |
| 1238 | |
| 1239 | if (readval) |
| 1240 | return regmap_read(st->regmap, reg, readval); |
| 1241 | else |
| 1242 | return regmap_write(st->regmap, reg, writeval); |
| 1243 | } |
| 1244 | |
| 1245 | static irqreturn_t ltc2983_irq_handler(int irq, void *data) |
| 1246 | { |
| 1247 | struct ltc2983_data *st = data; |
| 1248 | |
| 1249 | complete(&st->completion); |
| 1250 | return IRQ_HANDLED; |
| 1251 | } |
| 1252 | |
| 1253 | #define LTC2983_CHAN(__type, index, __address) ({ \ |
| 1254 | struct iio_chan_spec __chan = { \ |
| 1255 | .type = __type, \ |
| 1256 | .indexed = 1, \ |
| 1257 | .channel = index, \ |
| 1258 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 1259 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
| 1260 | .address = __address, \ |
| 1261 | }; \ |
| 1262 | __chan; \ |
| 1263 | }) |
| 1264 | |
| 1265 | static int ltc2983_parse_dt(struct ltc2983_data *st) |
| 1266 | { |
| 1267 | struct device_node *child; |
| 1268 | struct device *dev = &st->spi->dev; |
| 1269 | int ret = 0, chan = 0, channel_avail_mask = 0; |
| 1270 | |
| 1271 | of_property_read_u32(dev->of_node, "adi,mux-delay-config-us", |
| 1272 | &st->mux_delay_config); |
| 1273 | |
| 1274 | of_property_read_u32(dev->of_node, "adi,filter-notch-freq", |
| 1275 | &st->filter_notch_freq); |
| 1276 | |
| 1277 | st->num_channels = of_get_available_child_count(dev->of_node); |
Nuno Sá | 25d4abb | 2021-08-25 10:41:49 +0200 | [diff] [blame] | 1278 | if (!st->num_channels) { |
| 1279 | dev_err(&st->spi->dev, "At least one channel must be given!"); |
| 1280 | return -EINVAL; |
| 1281 | } |
| 1282 | |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1283 | st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors), |
| 1284 | GFP_KERNEL); |
| 1285 | if (!st->sensors) |
| 1286 | return -ENOMEM; |
| 1287 | |
| 1288 | st->iio_channels = st->num_channels; |
| 1289 | for_each_available_child_of_node(dev->of_node, child) { |
| 1290 | struct ltc2983_sensor sensor; |
| 1291 | |
| 1292 | ret = of_property_read_u32(child, "reg", &sensor.chan); |
| 1293 | if (ret) { |
| 1294 | dev_err(dev, "reg property must given for child nodes\n"); |
Nuno Sá | b07c47b | 2020-09-25 11:10:44 +0200 | [diff] [blame] | 1295 | goto put_child; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | /* check if we have a valid channel */ |
| 1299 | if (sensor.chan < LTC2983_MIN_CHANNELS_NR || |
| 1300 | sensor.chan > LTC2983_MAX_CHANNELS_NR) { |
Nuno Sá | b07c47b | 2020-09-25 11:10:44 +0200 | [diff] [blame] | 1301 | ret = -EINVAL; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1302 | dev_err(dev, |
| 1303 | "chan:%d must be from 1 to 20\n", sensor.chan); |
Nuno Sá | b07c47b | 2020-09-25 11:10:44 +0200 | [diff] [blame] | 1304 | goto put_child; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1305 | } else if (channel_avail_mask & BIT(sensor.chan)) { |
Nuno Sá | b07c47b | 2020-09-25 11:10:44 +0200 | [diff] [blame] | 1306 | ret = -EINVAL; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1307 | dev_err(dev, "chan:%d already in use\n", sensor.chan); |
Nuno Sá | b07c47b | 2020-09-25 11:10:44 +0200 | [diff] [blame] | 1308 | goto put_child; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | ret = of_property_read_u32(child, "adi,sensor-type", |
| 1312 | &sensor.type); |
| 1313 | if (ret) { |
| 1314 | dev_err(dev, |
| 1315 | "adi,sensor-type property must given for child nodes\n"); |
Nuno Sá | b07c47b | 2020-09-25 11:10:44 +0200 | [diff] [blame] | 1316 | goto put_child; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | dev_dbg(dev, "Create new sensor, type %u, chann %u", |
| 1320 | sensor.type, |
| 1321 | sensor.chan); |
| 1322 | |
| 1323 | if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE && |
| 1324 | sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) { |
| 1325 | st->sensors[chan] = ltc2983_thermocouple_new(child, st, |
| 1326 | &sensor); |
| 1327 | } else if (sensor.type >= LTC2983_SENSOR_RTD && |
| 1328 | sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) { |
| 1329 | st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor); |
| 1330 | } else if (sensor.type >= LTC2983_SENSOR_THERMISTOR && |
| 1331 | sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) { |
| 1332 | st->sensors[chan] = ltc2983_thermistor_new(child, st, |
| 1333 | &sensor); |
| 1334 | } else if (sensor.type == LTC2983_SENSOR_DIODE) { |
| 1335 | st->sensors[chan] = ltc2983_diode_new(child, st, |
| 1336 | &sensor); |
| 1337 | } else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) { |
| 1338 | st->sensors[chan] = ltc2983_r_sense_new(child, st, |
| 1339 | &sensor); |
| 1340 | /* don't add rsense to iio */ |
| 1341 | st->iio_channels--; |
| 1342 | } else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) { |
| 1343 | st->sensors[chan] = ltc2983_adc_new(child, st, &sensor); |
| 1344 | } else { |
| 1345 | dev_err(dev, "Unknown sensor type %d\n", sensor.type); |
Nuno Sá | b07c47b | 2020-09-25 11:10:44 +0200 | [diff] [blame] | 1346 | ret = -EINVAL; |
| 1347 | goto put_child; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | if (IS_ERR(st->sensors[chan])) { |
| 1351 | dev_err(dev, "Failed to create sensor %ld", |
| 1352 | PTR_ERR(st->sensors[chan])); |
Nuno Sá | b07c47b | 2020-09-25 11:10:44 +0200 | [diff] [blame] | 1353 | ret = PTR_ERR(st->sensors[chan]); |
| 1354 | goto put_child; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1355 | } |
| 1356 | /* set generic sensor parameters */ |
| 1357 | st->sensors[chan]->chan = sensor.chan; |
| 1358 | st->sensors[chan]->type = sensor.type; |
| 1359 | |
| 1360 | channel_avail_mask |= BIT(sensor.chan); |
| 1361 | chan++; |
| 1362 | } |
| 1363 | |
| 1364 | return 0; |
Nuno Sá | b07c47b | 2020-09-25 11:10:44 +0200 | [diff] [blame] | 1365 | put_child: |
| 1366 | of_node_put(child); |
| 1367 | return ret; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio) |
| 1371 | { |
Nuno Sá | b76d26d | 2021-08-11 15:32:20 +0200 | [diff] [blame] | 1372 | u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1373 | int ret; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1374 | |
Nuno Sá | b76d26d | 2021-08-11 15:32:20 +0200 | [diff] [blame] | 1375 | /* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */ |
| 1376 | ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status, |
| 1377 | LTC2983_STATUS_UP(status) == 1, 25000, |
| 1378 | 25000 * 10); |
| 1379 | if (ret) { |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1380 | dev_err(&st->spi->dev, "Device startup timed out\n"); |
Nuno Sá | b76d26d | 2021-08-11 15:32:20 +0200 | [diff] [blame] | 1381 | return ret; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | st->iio_chan = devm_kzalloc(&st->spi->dev, |
| 1385 | st->iio_channels * sizeof(*st->iio_chan), |
| 1386 | GFP_KERNEL); |
| 1387 | |
| 1388 | if (!st->iio_chan) |
| 1389 | return -ENOMEM; |
| 1390 | |
| 1391 | ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG, |
| 1392 | LTC2983_NOTCH_FREQ_MASK, |
| 1393 | LTC2983_NOTCH_FREQ(st->filter_notch_freq)); |
| 1394 | if (ret) |
| 1395 | return ret; |
| 1396 | |
| 1397 | ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG, |
| 1398 | st->mux_delay_config); |
| 1399 | if (ret) |
| 1400 | return ret; |
| 1401 | |
| 1402 | for (chan = 0; chan < st->num_channels; chan++) { |
| 1403 | u32 chan_type = 0, *iio_chan; |
| 1404 | |
| 1405 | ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]); |
| 1406 | if (ret) |
| 1407 | return ret; |
| 1408 | /* |
| 1409 | * The assign_iio flag is necessary for when the device is |
| 1410 | * coming out of sleep. In that case, we just need to |
| 1411 | * re-configure the device channels. |
| 1412 | * We also don't assign iio channels for rsense. |
| 1413 | */ |
| 1414 | if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR || |
| 1415 | !assign_iio) |
| 1416 | continue; |
| 1417 | |
| 1418 | /* assign iio channel */ |
| 1419 | if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) { |
| 1420 | chan_type = IIO_TEMP; |
| 1421 | iio_chan = &iio_chan_t; |
| 1422 | } else { |
| 1423 | chan_type = IIO_VOLTAGE; |
| 1424 | iio_chan = &iio_chan_v; |
| 1425 | } |
| 1426 | |
| 1427 | /* |
| 1428 | * add chan as the iio .address so that, we can directly |
| 1429 | * reference the sensor given the iio_chan_spec |
| 1430 | */ |
| 1431 | st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++, |
| 1432 | chan); |
| 1433 | } |
| 1434 | |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | static const struct regmap_range ltc2983_reg_ranges[] = { |
| 1439 | regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG), |
| 1440 | regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG), |
| 1441 | regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG), |
| 1442 | regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG, |
| 1443 | LTC2983_MULT_CHANNEL_END_REG), |
| 1444 | regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG), |
| 1445 | regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG, |
| 1446 | LTC2983_CHAN_ASSIGN_END_REG), |
| 1447 | regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG, |
| 1448 | LTC2983_CUST_SENS_TBL_END_REG), |
| 1449 | }; |
| 1450 | |
| 1451 | static const struct regmap_access_table ltc2983_reg_table = { |
| 1452 | .yes_ranges = ltc2983_reg_ranges, |
| 1453 | .n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges), |
| 1454 | }; |
| 1455 | |
| 1456 | /* |
| 1457 | * The reg_bits are actually 12 but the device needs the first *complete* |
| 1458 | * byte for the command (R/W). |
| 1459 | */ |
| 1460 | static const struct regmap_config ltc2983_regmap_config = { |
| 1461 | .reg_bits = 24, |
| 1462 | .val_bits = 8, |
| 1463 | .wr_table = <c2983_reg_table, |
| 1464 | .rd_table = <c2983_reg_table, |
| 1465 | .read_flag_mask = GENMASK(1, 0), |
| 1466 | .write_flag_mask = BIT(1), |
| 1467 | }; |
| 1468 | |
| 1469 | static const struct iio_info ltc2983_iio_info = { |
| 1470 | .read_raw = ltc2983_read_raw, |
| 1471 | .debugfs_reg_access = ltc2983_reg_access, |
| 1472 | }; |
| 1473 | |
| 1474 | static int ltc2983_probe(struct spi_device *spi) |
| 1475 | { |
| 1476 | struct ltc2983_data *st; |
| 1477 | struct iio_dev *indio_dev; |
Nuno Sá | 919726c | 2021-08-25 10:41:48 +0200 | [diff] [blame] | 1478 | struct gpio_desc *gpio; |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1479 | const char *name = spi_get_device_id(spi)->name; |
| 1480 | int ret; |
| 1481 | |
| 1482 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); |
| 1483 | if (!indio_dev) |
| 1484 | return -ENOMEM; |
| 1485 | |
| 1486 | st = iio_priv(indio_dev); |
| 1487 | |
| 1488 | st->regmap = devm_regmap_init_spi(spi, <c2983_regmap_config); |
| 1489 | if (IS_ERR(st->regmap)) { |
| 1490 | dev_err(&spi->dev, "Failed to initialize regmap\n"); |
| 1491 | return PTR_ERR(st->regmap); |
| 1492 | } |
| 1493 | |
| 1494 | mutex_init(&st->lock); |
| 1495 | init_completion(&st->completion); |
| 1496 | st->spi = spi; |
| 1497 | spi_set_drvdata(spi, st); |
| 1498 | |
| 1499 | ret = ltc2983_parse_dt(st); |
| 1500 | if (ret) |
| 1501 | return ret; |
Nuno Sá | b76d26d | 2021-08-11 15:32:20 +0200 | [diff] [blame] | 1502 | |
Nuno Sá | 919726c | 2021-08-25 10:41:48 +0200 | [diff] [blame] | 1503 | gpio = devm_gpiod_get_optional(&st->spi->dev, "reset", GPIOD_OUT_HIGH); |
| 1504 | if (IS_ERR(gpio)) |
| 1505 | return PTR_ERR(gpio); |
| 1506 | |
| 1507 | if (gpio) { |
| 1508 | /* bring the device out of reset */ |
| 1509 | usleep_range(1000, 1200); |
| 1510 | gpiod_set_value_cansleep(gpio, 0); |
| 1511 | } |
| 1512 | |
Nuno Sá | b76d26d | 2021-08-11 15:32:20 +0200 | [diff] [blame] | 1513 | ret = ltc2983_setup(st, true); |
| 1514 | if (ret) |
| 1515 | return ret; |
| 1516 | |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1517 | ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler, |
| 1518 | IRQF_TRIGGER_RISING, name, st); |
| 1519 | if (ret) { |
| 1520 | dev_err(&spi->dev, "failed to request an irq, %d", ret); |
| 1521 | return ret; |
| 1522 | } |
| 1523 | |
Nuno Sá | f110f31 | 2019-10-11 10:40:37 +0200 | [diff] [blame] | 1524 | indio_dev->name = name; |
| 1525 | indio_dev->num_channels = st->iio_channels; |
| 1526 | indio_dev->channels = st->iio_chan; |
| 1527 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 1528 | indio_dev->info = <c2983_iio_info; |
| 1529 | |
| 1530 | return devm_iio_device_register(&spi->dev, indio_dev); |
| 1531 | } |
| 1532 | |
| 1533 | static int __maybe_unused ltc2983_resume(struct device *dev) |
| 1534 | { |
| 1535 | struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev)); |
| 1536 | int dummy; |
| 1537 | |
| 1538 | /* dummy read to bring the device out of sleep */ |
| 1539 | regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy); |
| 1540 | /* we need to re-assign the channels */ |
| 1541 | return ltc2983_setup(st, false); |
| 1542 | } |
| 1543 | |
| 1544 | static int __maybe_unused ltc2983_suspend(struct device *dev) |
| 1545 | { |
| 1546 | struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev)); |
| 1547 | |
| 1548 | return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP); |
| 1549 | } |
| 1550 | |
| 1551 | static SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend, ltc2983_resume); |
| 1552 | |
| 1553 | static const struct spi_device_id ltc2983_id_table[] = { |
| 1554 | { "ltc2983" }, |
| 1555 | {}, |
| 1556 | }; |
| 1557 | MODULE_DEVICE_TABLE(spi, ltc2983_id_table); |
| 1558 | |
| 1559 | static const struct of_device_id ltc2983_of_match[] = { |
| 1560 | { .compatible = "adi,ltc2983" }, |
| 1561 | {}, |
| 1562 | }; |
| 1563 | MODULE_DEVICE_TABLE(of, ltc2983_of_match); |
| 1564 | |
| 1565 | static struct spi_driver ltc2983_driver = { |
| 1566 | .driver = { |
| 1567 | .name = "ltc2983", |
| 1568 | .of_match_table = ltc2983_of_match, |
| 1569 | .pm = <c2983_pm_ops, |
| 1570 | }, |
| 1571 | .probe = ltc2983_probe, |
| 1572 | .id_table = ltc2983_id_table, |
| 1573 | }; |
| 1574 | |
| 1575 | module_spi_driver(ltc2983_driver); |
| 1576 | |
| 1577 | MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>"); |
| 1578 | MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors"); |
| 1579 | MODULE_LICENSE("GPL"); |