12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583 |
- // SPDX-License-Identifier: GPL-2.0
- /*
- * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System
- * driver
- *
- * Copyright 2019 Analog Devices Inc.
- */
- #include <linux/bitfield.h>
- #include <linux/completion.h>
- #include <linux/device.h>
- #include <linux/kernel.h>
- #include <linux/iio/iio.h>
- #include <linux/interrupt.h>
- #include <linux/list.h>
- #include <linux/mod_devicetable.h>
- #include <linux/module.h>
- #include <linux/property.h>
- #include <linux/regmap.h>
- #include <linux/spi/spi.h>
- #include <asm/byteorder.h>
- #include <asm/unaligned.h>
- /* register map */
- #define LTC2983_STATUS_REG 0x0000
- #define LTC2983_TEMP_RES_START_REG 0x0010
- #define LTC2983_TEMP_RES_END_REG 0x005F
- #define LTC2983_GLOBAL_CONFIG_REG 0x00F0
- #define LTC2983_MULT_CHANNEL_START_REG 0x00F4
- #define LTC2983_MULT_CHANNEL_END_REG 0x00F7
- #define LTC2983_MUX_CONFIG_REG 0x00FF
- #define LTC2983_CHAN_ASSIGN_START_REG 0x0200
- #define LTC2983_CHAN_ASSIGN_END_REG 0x024F
- #define LTC2983_CUST_SENS_TBL_START_REG 0x0250
- #define LTC2983_CUST_SENS_TBL_END_REG 0x03CF
- #define LTC2983_DIFFERENTIAL_CHAN_MIN 2
- #define LTC2983_MAX_CHANNELS_NR 20
- #define LTC2983_MIN_CHANNELS_NR 1
- #define LTC2983_SLEEP 0x97
- #define LTC2983_CUSTOM_STEINHART_SIZE 24
- #define LTC2983_CUSTOM_SENSOR_ENTRY_SZ 6
- #define LTC2983_CUSTOM_STEINHART_ENTRY_SZ 4
- #define LTC2983_CHAN_START_ADDR(chan) \
- (((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG)
- #define LTC2983_CHAN_RES_ADDR(chan) \
- (((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG)
- #define LTC2983_THERMOCOUPLE_DIFF_MASK BIT(3)
- #define LTC2983_THERMOCOUPLE_SGL(x) \
- FIELD_PREP(LTC2983_THERMOCOUPLE_DIFF_MASK, x)
- #define LTC2983_THERMOCOUPLE_OC_CURR_MASK GENMASK(1, 0)
- #define LTC2983_THERMOCOUPLE_OC_CURR(x) \
- FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CURR_MASK, x)
- #define LTC2983_THERMOCOUPLE_OC_CHECK_MASK BIT(2)
- #define LTC2983_THERMOCOUPLE_OC_CHECK(x) \
- FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CHECK_MASK, x)
- #define LTC2983_THERMISTOR_DIFF_MASK BIT(2)
- #define LTC2983_THERMISTOR_SGL(x) \
- FIELD_PREP(LTC2983_THERMISTOR_DIFF_MASK, x)
- #define LTC2983_THERMISTOR_R_SHARE_MASK BIT(1)
- #define LTC2983_THERMISTOR_R_SHARE(x) \
- FIELD_PREP(LTC2983_THERMISTOR_R_SHARE_MASK, x)
- #define LTC2983_THERMISTOR_C_ROTATE_MASK BIT(0)
- #define LTC2983_THERMISTOR_C_ROTATE(x) \
- FIELD_PREP(LTC2983_THERMISTOR_C_ROTATE_MASK, x)
- #define LTC2983_DIODE_DIFF_MASK BIT(2)
- #define LTC2983_DIODE_SGL(x) \
- FIELD_PREP(LTC2983_DIODE_DIFF_MASK, x)
- #define LTC2983_DIODE_3_CONV_CYCLE_MASK BIT(1)
- #define LTC2983_DIODE_3_CONV_CYCLE(x) \
- FIELD_PREP(LTC2983_DIODE_3_CONV_CYCLE_MASK, x)
- #define LTC2983_DIODE_AVERAGE_ON_MASK BIT(0)
- #define LTC2983_DIODE_AVERAGE_ON(x) \
- FIELD_PREP(LTC2983_DIODE_AVERAGE_ON_MASK, x)
- #define LTC2983_RTD_4_WIRE_MASK BIT(3)
- #define LTC2983_RTD_ROTATION_MASK BIT(1)
- #define LTC2983_RTD_C_ROTATE(x) \
- FIELD_PREP(LTC2983_RTD_ROTATION_MASK, x)
- #define LTC2983_RTD_KELVIN_R_SENSE_MASK GENMASK(3, 2)
- #define LTC2983_RTD_N_WIRES_MASK GENMASK(3, 2)
- #define LTC2983_RTD_N_WIRES(x) \
- FIELD_PREP(LTC2983_RTD_N_WIRES_MASK, x)
- #define LTC2983_RTD_R_SHARE_MASK BIT(0)
- #define LTC2983_RTD_R_SHARE(x) \
- FIELD_PREP(LTC2983_RTD_R_SHARE_MASK, 1)
- #define LTC2983_COMMON_HARD_FAULT_MASK GENMASK(31, 30)
- #define LTC2983_COMMON_SOFT_FAULT_MASK GENMASK(27, 25)
- #define LTC2983_STATUS_START_MASK BIT(7)
- #define LTC2983_STATUS_START(x) FIELD_PREP(LTC2983_STATUS_START_MASK, x)
- #define LTC2983_STATUS_UP_MASK GENMASK(7, 6)
- #define LTC2983_STATUS_UP(reg) FIELD_GET(LTC2983_STATUS_UP_MASK, reg)
- #define LTC2983_STATUS_CHAN_SEL_MASK GENMASK(4, 0)
- #define LTC2983_STATUS_CHAN_SEL(x) \
- FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x)
- #define LTC2983_TEMP_UNITS_MASK BIT(2)
- #define LTC2983_TEMP_UNITS(x) FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x)
- #define LTC2983_NOTCH_FREQ_MASK GENMASK(1, 0)
- #define LTC2983_NOTCH_FREQ(x) FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x)
- #define LTC2983_RES_VALID_MASK BIT(24)
- #define LTC2983_DATA_MASK GENMASK(23, 0)
- #define LTC2983_DATA_SIGN_BIT 23
- #define LTC2983_CHAN_TYPE_MASK GENMASK(31, 27)
- #define LTC2983_CHAN_TYPE(x) FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x)
- /* cold junction for thermocouples and rsense for rtd's and thermistor's */
- #define LTC2983_CHAN_ASSIGN_MASK GENMASK(26, 22)
- #define LTC2983_CHAN_ASSIGN(x) FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x)
- #define LTC2983_CUSTOM_LEN_MASK GENMASK(5, 0)
- #define LTC2983_CUSTOM_LEN(x) FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x)
- #define LTC2983_CUSTOM_ADDR_MASK GENMASK(11, 6)
- #define LTC2983_CUSTOM_ADDR(x) FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x)
- #define LTC2983_THERMOCOUPLE_CFG_MASK GENMASK(21, 18)
- #define LTC2983_THERMOCOUPLE_CFG(x) \
- FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x)
- #define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK GENMASK(31, 29)
- #define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK GENMASK(28, 25)
- #define LTC2983_RTD_CFG_MASK GENMASK(21, 18)
- #define LTC2983_RTD_CFG(x) FIELD_PREP(LTC2983_RTD_CFG_MASK, x)
- #define LTC2983_RTD_EXC_CURRENT_MASK GENMASK(17, 14)
- #define LTC2983_RTD_EXC_CURRENT(x) \
- FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x)
- #define LTC2983_RTD_CURVE_MASK GENMASK(13, 12)
- #define LTC2983_RTD_CURVE(x) FIELD_PREP(LTC2983_RTD_CURVE_MASK, x)
- #define LTC2983_THERMISTOR_CFG_MASK GENMASK(21, 19)
- #define LTC2983_THERMISTOR_CFG(x) \
- FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x)
- #define LTC2983_THERMISTOR_EXC_CURRENT_MASK GENMASK(18, 15)
- #define LTC2983_THERMISTOR_EXC_CURRENT(x) \
- FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x)
- #define LTC2983_DIODE_CFG_MASK GENMASK(26, 24)
- #define LTC2983_DIODE_CFG(x) FIELD_PREP(LTC2983_DIODE_CFG_MASK, x)
- #define LTC2983_DIODE_EXC_CURRENT_MASK GENMASK(23, 22)
- #define LTC2983_DIODE_EXC_CURRENT(x) \
- FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x)
- #define LTC2983_DIODE_IDEAL_FACTOR_MASK GENMASK(21, 0)
- #define LTC2983_DIODE_IDEAL_FACTOR(x) \
- FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x)
- #define LTC2983_R_SENSE_VAL_MASK GENMASK(26, 0)
- #define LTC2983_R_SENSE_VAL(x) FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x)
- #define LTC2983_ADC_SINGLE_ENDED_MASK BIT(26)
- #define LTC2983_ADC_SINGLE_ENDED(x) \
- FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x)
- enum {
- LTC2983_SENSOR_THERMOCOUPLE = 1,
- LTC2983_SENSOR_THERMOCOUPLE_CUSTOM = 9,
- LTC2983_SENSOR_RTD = 10,
- LTC2983_SENSOR_RTD_CUSTOM = 18,
- LTC2983_SENSOR_THERMISTOR = 19,
- LTC2983_SENSOR_THERMISTOR_STEINHART = 26,
- LTC2983_SENSOR_THERMISTOR_CUSTOM = 27,
- LTC2983_SENSOR_DIODE = 28,
- LTC2983_SENSOR_SENSE_RESISTOR = 29,
- LTC2983_SENSOR_DIRECT_ADC = 30,
- };
- #define to_thermocouple(_sensor) \
- container_of(_sensor, struct ltc2983_thermocouple, sensor)
- #define to_rtd(_sensor) \
- container_of(_sensor, struct ltc2983_rtd, sensor)
- #define to_thermistor(_sensor) \
- container_of(_sensor, struct ltc2983_thermistor, sensor)
- #define to_diode(_sensor) \
- container_of(_sensor, struct ltc2983_diode, sensor)
- #define to_rsense(_sensor) \
- container_of(_sensor, struct ltc2983_rsense, sensor)
- #define to_adc(_sensor) \
- container_of(_sensor, struct ltc2983_adc, sensor)
- struct ltc2983_data {
- struct regmap *regmap;
- struct spi_device *spi;
- struct mutex lock;
- struct completion completion;
- struct iio_chan_spec *iio_chan;
- struct ltc2983_sensor **sensors;
- u32 mux_delay_config;
- u32 filter_notch_freq;
- u16 custom_table_size;
- u8 num_channels;
- u8 iio_channels;
- /*
- * DMA (thus cache coherency maintenance) may require the
- * transfer buffers to live in their own cache lines.
- * Holds the converted temperature
- */
- __be32 temp __aligned(IIO_DMA_MINALIGN);
- __be32 chan_val;
- };
- struct ltc2983_sensor {
- int (*fault_handler)(const struct ltc2983_data *st, const u32 result);
- int (*assign_chan)(struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor);
- /* specifies the sensor channel */
- u32 chan;
- /* sensor type */
- u32 type;
- };
- struct ltc2983_custom_sensor {
- /* raw table sensor data */
- void *table;
- size_t size;
- /* address offset */
- s8 offset;
- bool is_steinhart;
- };
- struct ltc2983_thermocouple {
- struct ltc2983_sensor sensor;
- struct ltc2983_custom_sensor *custom;
- u32 sensor_config;
- u32 cold_junction_chan;
- };
- struct ltc2983_rtd {
- struct ltc2983_sensor sensor;
- struct ltc2983_custom_sensor *custom;
- u32 sensor_config;
- u32 r_sense_chan;
- u32 excitation_current;
- u32 rtd_curve;
- };
- struct ltc2983_thermistor {
- struct ltc2983_sensor sensor;
- struct ltc2983_custom_sensor *custom;
- u32 sensor_config;
- u32 r_sense_chan;
- u32 excitation_current;
- };
- struct ltc2983_diode {
- struct ltc2983_sensor sensor;
- u32 sensor_config;
- u32 excitation_current;
- u32 ideal_factor_value;
- };
- struct ltc2983_rsense {
- struct ltc2983_sensor sensor;
- u32 r_sense_val;
- };
- struct ltc2983_adc {
- struct ltc2983_sensor sensor;
- bool single_ended;
- };
- /*
- * Convert to Q format numbers. These number's are integers where
- * the number of integer and fractional bits are specified. The resolution
- * is given by 1/@resolution and tell us the number of fractional bits. For
- * instance a resolution of 2^-10 means we have 10 fractional bits.
- */
- static u32 __convert_to_raw(const u64 val, const u32 resolution)
- {
- u64 __res = val * resolution;
- /* all values are multiplied by 1000000 to remove the fraction */
- do_div(__res, 1000000);
- return __res;
- }
- static u32 __convert_to_raw_sign(const u64 val, const u32 resolution)
- {
- s64 __res = -(s32)val;
- __res = __convert_to_raw(__res, resolution);
- return (u32)-__res;
- }
- static int __ltc2983_fault_handler(const struct ltc2983_data *st,
- const u32 result, const u32 hard_mask,
- const u32 soft_mask)
- {
- const struct device *dev = &st->spi->dev;
- if (result & hard_mask) {
- dev_err(dev, "Invalid conversion: Sensor HARD fault\n");
- return -EIO;
- } else if (result & soft_mask) {
- /* just print a warning */
- dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n");
- }
- return 0;
- }
- static int __ltc2983_chan_assign_common(struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor,
- u32 chan_val)
- {
- u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan);
- chan_val |= LTC2983_CHAN_TYPE(sensor->type);
- dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg,
- chan_val);
- st->chan_val = cpu_to_be32(chan_val);
- return regmap_bulk_write(st->regmap, reg, &st->chan_val,
- sizeof(st->chan_val));
- }
- static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
- struct ltc2983_custom_sensor *custom,
- u32 *chan_val)
- {
- u32 reg;
- u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ :
- LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
- const struct device *dev = &st->spi->dev;
- /*
- * custom->size holds the raw size of the table. However, when
- * configuring the sensor channel, we must write the number of
- * entries of the table minus 1. For steinhart sensors 0 is written
- * since the size is constant!
- */
- const u8 len = custom->is_steinhart ? 0 :
- (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1;
- /*
- * Check if the offset was assigned already. It should be for steinhart
- * sensors. When coming from sleep, it should be assigned for all.
- */
- if (custom->offset < 0) {
- /*
- * This needs to be done again here because, from the moment
- * when this test was done (successfully) for this custom
- * sensor, a steinhart sensor might have been added changing
- * custom_table_size...
- */
- if (st->custom_table_size + custom->size >
- (LTC2983_CUST_SENS_TBL_END_REG -
- LTC2983_CUST_SENS_TBL_START_REG) + 1) {
- dev_err(dev,
- "Not space left(%d) for new custom sensor(%zu)",
- st->custom_table_size,
- custom->size);
- return -EINVAL;
- }
- custom->offset = st->custom_table_size /
- LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
- st->custom_table_size += custom->size;
- }
- reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG;
- *chan_val |= LTC2983_CUSTOM_LEN(len);
- *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset);
- dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu",
- reg, custom->offset,
- custom->size);
- /* write custom sensor table */
- return regmap_bulk_write(st->regmap, reg, custom->table, custom->size);
- }
- static struct ltc2983_custom_sensor *
- __ltc2983_custom_sensor_new(struct ltc2983_data *st, const struct fwnode_handle *fn,
- const char *propname, const bool is_steinhart,
- const u32 resolution, const bool has_signed)
- {
- struct ltc2983_custom_sensor *new_custom;
- struct device *dev = &st->spi->dev;
- /*
- * For custom steinhart, the full u32 is taken. For all the others
- * the MSB is discarded.
- */
- const u8 n_size = is_steinhart ? 4 : 3;
- u8 index, n_entries;
- int ret;
- if (is_steinhart)
- n_entries = fwnode_property_count_u32(fn, propname);
- else
- n_entries = fwnode_property_count_u64(fn, propname);
- /* n_entries must be an even number */
- if (!n_entries || (n_entries % 2) != 0) {
- dev_err(dev, "Number of entries either 0 or not even\n");
- return ERR_PTR(-EINVAL);
- }
- new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL);
- if (!new_custom)
- return ERR_PTR(-ENOMEM);
- new_custom->size = n_entries * n_size;
- /* check Steinhart size */
- if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE) {
- dev_err(dev, "Steinhart sensors size(%zu) must be %u\n", new_custom->size,
- LTC2983_CUSTOM_STEINHART_SIZE);
- return ERR_PTR(-EINVAL);
- }
- /* Check space on the table. */
- if (st->custom_table_size + new_custom->size >
- (LTC2983_CUST_SENS_TBL_END_REG -
- LTC2983_CUST_SENS_TBL_START_REG) + 1) {
- dev_err(dev, "No space left(%d) for new custom sensor(%zu)",
- st->custom_table_size, new_custom->size);
- return ERR_PTR(-EINVAL);
- }
- /* allocate the table */
- if (is_steinhart)
- new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u32), GFP_KERNEL);
- else
- new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u64), GFP_KERNEL);
- if (!new_custom->table)
- return ERR_PTR(-ENOMEM);
- /*
- * Steinhart sensors are configured with raw values in the firmware
- * node. For the other sensors we must convert the value to raw.
- * The odd index's correspond to temperatures and always have 1/1024
- * of resolution. Temperatures also come in Kelvin, so signed values
- * are not possible.
- */
- if (is_steinhart) {
- ret = fwnode_property_read_u32_array(fn, propname, new_custom->table, n_entries);
- if (ret < 0)
- return ERR_PTR(ret);
- cpu_to_be32_array(new_custom->table, new_custom->table, n_entries);
- } else {
- ret = fwnode_property_read_u64_array(fn, propname, new_custom->table, n_entries);
- if (ret < 0)
- return ERR_PTR(ret);
- for (index = 0; index < n_entries; index++) {
- u64 temp = ((u64 *)new_custom->table)[index];
- if ((index % 2) != 0)
- temp = __convert_to_raw(temp, 1024);
- else if (has_signed && (s64)temp < 0)
- temp = __convert_to_raw_sign(temp, resolution);
- else
- temp = __convert_to_raw(temp, resolution);
- put_unaligned_be24(temp, new_custom->table + index * 3);
- }
- }
- new_custom->is_steinhart = is_steinhart;
- /*
- * This is done to first add all the steinhart sensors to the table,
- * in order to maximize the table usage. If we mix adding steinhart
- * with the other sensors, we might have to do some roundup to make
- * sure that sensor_addr - 0x250(start address) is a multiple of 4
- * (for steinhart), and a multiple of 6 for all the other sensors.
- * Since we have const 24 bytes for steinhart sensors and 24 is
- * also a multiple of 6, we guarantee that the first non-steinhart
- * sensor will sit in a correct address without the need of filling
- * addresses.
- */
- if (is_steinhart) {
- new_custom->offset = st->custom_table_size /
- LTC2983_CUSTOM_STEINHART_ENTRY_SZ;
- st->custom_table_size += new_custom->size;
- } else {
- /* mark as unset. This is checked later on the assign phase */
- new_custom->offset = -1;
- }
- return new_custom;
- }
- static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st,
- const u32 result)
- {
- return __ltc2983_fault_handler(st, result,
- LTC2983_THERMOCOUPLE_HARD_FAULT_MASK,
- LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK);
- }
- static int ltc2983_common_fault_handler(const struct ltc2983_data *st,
- const u32 result)
- {
- return __ltc2983_fault_handler(st, result,
- LTC2983_COMMON_HARD_FAULT_MASK,
- LTC2983_COMMON_SOFT_FAULT_MASK);
- }
- static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor)
- {
- struct ltc2983_thermocouple *thermo = to_thermocouple(sensor);
- u32 chan_val;
- chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan);
- chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config);
- if (thermo->custom) {
- int ret;
- ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom,
- &chan_val);
- if (ret)
- return ret;
- }
- return __ltc2983_chan_assign_common(st, sensor, chan_val);
- }
- static int ltc2983_rtd_assign_chan(struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor)
- {
- struct ltc2983_rtd *rtd = to_rtd(sensor);
- u32 chan_val;
- chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan);
- chan_val |= LTC2983_RTD_CFG(rtd->sensor_config);
- chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current);
- chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve);
- if (rtd->custom) {
- int ret;
- ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom,
- &chan_val);
- if (ret)
- return ret;
- }
- return __ltc2983_chan_assign_common(st, sensor, chan_val);
- }
- static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor)
- {
- struct ltc2983_thermistor *thermistor = to_thermistor(sensor);
- u32 chan_val;
- chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan);
- chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config);
- chan_val |=
- LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current);
- if (thermistor->custom) {
- int ret;
- ret = __ltc2983_chan_custom_sensor_assign(st,
- thermistor->custom,
- &chan_val);
- if (ret)
- return ret;
- }
- return __ltc2983_chan_assign_common(st, sensor, chan_val);
- }
- static int ltc2983_diode_assign_chan(struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor)
- {
- struct ltc2983_diode *diode = to_diode(sensor);
- u32 chan_val;
- chan_val = LTC2983_DIODE_CFG(diode->sensor_config);
- chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current);
- chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value);
- return __ltc2983_chan_assign_common(st, sensor, chan_val);
- }
- static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor)
- {
- struct ltc2983_rsense *rsense = to_rsense(sensor);
- u32 chan_val;
- chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val);
- return __ltc2983_chan_assign_common(st, sensor, chan_val);
- }
- static int ltc2983_adc_assign_chan(struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor)
- {
- struct ltc2983_adc *adc = to_adc(sensor);
- u32 chan_val;
- chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended);
- return __ltc2983_chan_assign_common(st, sensor, chan_val);
- }
- static struct ltc2983_sensor *
- ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor)
- {
- struct ltc2983_thermocouple *thermo;
- struct fwnode_handle *ref;
- u32 oc_current;
- int ret;
- thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL);
- if (!thermo)
- return ERR_PTR(-ENOMEM);
- if (fwnode_property_read_bool(child, "adi,single-ended"))
- thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1);
- ret = fwnode_property_read_u32(child, "adi,sensor-oc-current-microamp", &oc_current);
- if (!ret) {
- switch (oc_current) {
- case 10:
- thermo->sensor_config |=
- LTC2983_THERMOCOUPLE_OC_CURR(0);
- break;
- case 100:
- thermo->sensor_config |=
- LTC2983_THERMOCOUPLE_OC_CURR(1);
- break;
- case 500:
- thermo->sensor_config |=
- LTC2983_THERMOCOUPLE_OC_CURR(2);
- break;
- case 1000:
- thermo->sensor_config |=
- LTC2983_THERMOCOUPLE_OC_CURR(3);
- break;
- default:
- dev_err(&st->spi->dev,
- "Invalid open circuit current:%u", oc_current);
- return ERR_PTR(-EINVAL);
- }
- thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1);
- }
- /* validate channel index */
- if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) &&
- sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
- dev_err(&st->spi->dev,
- "Invalid chann:%d for differential thermocouple",
- sensor->chan);
- return ERR_PTR(-EINVAL);
- }
- ref = fwnode_find_reference(child, "adi,cold-junction-handle", 0);
- if (IS_ERR(ref)) {
- ref = NULL;
- } else {
- ret = fwnode_property_read_u32(ref, "reg", &thermo->cold_junction_chan);
- if (ret) {
- /*
- * This would be catched later but we can just return
- * the error right away.
- */
- dev_err(&st->spi->dev, "Property reg must be given\n");
- goto fail;
- }
- }
- /* check custom sensor */
- if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
- const char *propname = "adi,custom-thermocouple";
- thermo->custom = __ltc2983_custom_sensor_new(st, child,
- propname, false,
- 16384, true);
- if (IS_ERR(thermo->custom)) {
- ret = PTR_ERR(thermo->custom);
- goto fail;
- }
- }
- /* set common parameters */
- thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler;
- thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan;
- fwnode_handle_put(ref);
- return &thermo->sensor;
- fail:
- fwnode_handle_put(ref);
- return ERR_PTR(ret);
- }
- static struct ltc2983_sensor *
- ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor)
- {
- struct ltc2983_rtd *rtd;
- int ret = 0;
- struct device *dev = &st->spi->dev;
- struct fwnode_handle *ref;
- u32 excitation_current = 0, n_wires = 0;
- rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
- if (!rtd)
- return ERR_PTR(-ENOMEM);
- ref = fwnode_find_reference(child, "adi,rsense-handle", 0);
- if (IS_ERR(ref)) {
- dev_err(dev, "Property adi,rsense-handle missing or invalid");
- return ERR_CAST(ref);
- }
- ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan);
- if (ret) {
- dev_err(dev, "Property reg must be given\n");
- goto fail;
- }
- ret = fwnode_property_read_u32(child, "adi,number-of-wires", &n_wires);
- if (!ret) {
- switch (n_wires) {
- case 2:
- rtd->sensor_config = LTC2983_RTD_N_WIRES(0);
- break;
- case 3:
- rtd->sensor_config = LTC2983_RTD_N_WIRES(1);
- break;
- case 4:
- rtd->sensor_config = LTC2983_RTD_N_WIRES(2);
- break;
- case 5:
- /* 4 wires, Kelvin Rsense */
- rtd->sensor_config = LTC2983_RTD_N_WIRES(3);
- break;
- default:
- dev_err(dev, "Invalid number of wires:%u\n", n_wires);
- ret = -EINVAL;
- goto fail;
- }
- }
- if (fwnode_property_read_bool(child, "adi,rsense-share")) {
- /* Current rotation is only available with rsense sharing */
- if (fwnode_property_read_bool(child, "adi,current-rotate")) {
- if (n_wires == 2 || n_wires == 3) {
- dev_err(dev,
- "Rotation not allowed for 2/3 Wire RTDs");
- ret = -EINVAL;
- goto fail;
- }
- rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1);
- } else {
- rtd->sensor_config |= LTC2983_RTD_R_SHARE(1);
- }
- }
- /*
- * rtd channel indexes are a bit more complicated to validate.
- * For 4wire RTD with rotation, the channel selection cannot be
- * >=19 since the chann + 1 is used in this configuration.
- * For 4wire RTDs with kelvin rsense, the rsense channel cannot be
- * <=1 since chanel - 1 and channel - 2 are used.
- */
- if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) {
- /* 4-wire */
- u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN,
- max = LTC2983_MAX_CHANNELS_NR;
- if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK)
- max = LTC2983_MAX_CHANNELS_NR - 1;
- if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK)
- == LTC2983_RTD_KELVIN_R_SENSE_MASK) &&
- (rtd->r_sense_chan <= min)) {
- /* kelvin rsense*/
- dev_err(dev,
- "Invalid rsense chann:%d to use in kelvin rsense",
- rtd->r_sense_chan);
- ret = -EINVAL;
- goto fail;
- }
- if (sensor->chan < min || sensor->chan > max) {
- dev_err(dev, "Invalid chann:%d for the rtd config",
- sensor->chan);
- ret = -EINVAL;
- goto fail;
- }
- } else {
- /* same as differential case */
- if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
- dev_err(&st->spi->dev,
- "Invalid chann:%d for RTD", sensor->chan);
- ret = -EINVAL;
- goto fail;
- }
- }
- /* check custom sensor */
- if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
- rtd->custom = __ltc2983_custom_sensor_new(st, child,
- "adi,custom-rtd",
- false, 2048, false);
- if (IS_ERR(rtd->custom)) {
- ret = PTR_ERR(rtd->custom);
- goto fail;
- }
- }
- /* set common parameters */
- rtd->sensor.fault_handler = ltc2983_common_fault_handler;
- rtd->sensor.assign_chan = ltc2983_rtd_assign_chan;
- ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
- &excitation_current);
- if (ret) {
- /* default to 5uA */
- rtd->excitation_current = 1;
- } else {
- switch (excitation_current) {
- case 5:
- rtd->excitation_current = 0x01;
- break;
- case 10:
- rtd->excitation_current = 0x02;
- break;
- case 25:
- rtd->excitation_current = 0x03;
- break;
- case 50:
- rtd->excitation_current = 0x04;
- break;
- case 100:
- rtd->excitation_current = 0x05;
- break;
- case 250:
- rtd->excitation_current = 0x06;
- break;
- case 500:
- rtd->excitation_current = 0x07;
- break;
- case 1000:
- rtd->excitation_current = 0x08;
- break;
- default:
- dev_err(&st->spi->dev,
- "Invalid value for excitation current(%u)",
- excitation_current);
- ret = -EINVAL;
- goto fail;
- }
- }
- fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve);
- fwnode_handle_put(ref);
- return &rtd->sensor;
- fail:
- fwnode_handle_put(ref);
- return ERR_PTR(ret);
- }
- static struct ltc2983_sensor *
- ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor)
- {
- struct ltc2983_thermistor *thermistor;
- struct device *dev = &st->spi->dev;
- struct fwnode_handle *ref;
- u32 excitation_current = 0;
- int ret = 0;
- thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL);
- if (!thermistor)
- return ERR_PTR(-ENOMEM);
- ref = fwnode_find_reference(child, "adi,rsense-handle", 0);
- if (IS_ERR(ref)) {
- dev_err(dev, "Property adi,rsense-handle missing or invalid");
- return ERR_CAST(ref);
- }
- ret = fwnode_property_read_u32(ref, "reg", &thermistor->r_sense_chan);
- if (ret) {
- dev_err(dev, "rsense channel must be configured...\n");
- goto fail;
- }
- if (fwnode_property_read_bool(child, "adi,single-ended")) {
- thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1);
- } else if (fwnode_property_read_bool(child, "adi,rsense-share")) {
- /* rotation is only possible if sharing rsense */
- if (fwnode_property_read_bool(child, "adi,current-rotate"))
- thermistor->sensor_config =
- LTC2983_THERMISTOR_C_ROTATE(1);
- else
- thermistor->sensor_config =
- LTC2983_THERMISTOR_R_SHARE(1);
- }
- /* validate channel index */
- if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) &&
- sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
- dev_err(&st->spi->dev,
- "Invalid chann:%d for differential thermistor",
- sensor->chan);
- ret = -EINVAL;
- goto fail;
- }
- /* check custom sensor */
- if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
- bool steinhart = false;
- const char *propname;
- if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
- steinhart = true;
- propname = "adi,custom-steinhart";
- } else {
- propname = "adi,custom-thermistor";
- }
- thermistor->custom = __ltc2983_custom_sensor_new(st, child,
- propname,
- steinhart,
- 64, false);
- if (IS_ERR(thermistor->custom)) {
- ret = PTR_ERR(thermistor->custom);
- goto fail;
- }
- }
- /* set common parameters */
- thermistor->sensor.fault_handler = ltc2983_common_fault_handler;
- thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan;
- ret = fwnode_property_read_u32(child, "adi,excitation-current-nanoamp",
- &excitation_current);
- if (ret) {
- /* Auto range is not allowed for custom sensors */
- if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
- /* default to 1uA */
- thermistor->excitation_current = 0x03;
- else
- /* default to auto-range */
- thermistor->excitation_current = 0x0c;
- } else {
- switch (excitation_current) {
- case 0:
- /* auto range */
- if (sensor->type >=
- LTC2983_SENSOR_THERMISTOR_STEINHART) {
- dev_err(&st->spi->dev,
- "Auto Range not allowed for custom sensors\n");
- ret = -EINVAL;
- goto fail;
- }
- thermistor->excitation_current = 0x0c;
- break;
- case 250:
- thermistor->excitation_current = 0x01;
- break;
- case 500:
- thermistor->excitation_current = 0x02;
- break;
- case 1000:
- thermistor->excitation_current = 0x03;
- break;
- case 5000:
- thermistor->excitation_current = 0x04;
- break;
- case 10000:
- thermistor->excitation_current = 0x05;
- break;
- case 25000:
- thermistor->excitation_current = 0x06;
- break;
- case 50000:
- thermistor->excitation_current = 0x07;
- break;
- case 100000:
- thermistor->excitation_current = 0x08;
- break;
- case 250000:
- thermistor->excitation_current = 0x09;
- break;
- case 500000:
- thermistor->excitation_current = 0x0a;
- break;
- case 1000000:
- thermistor->excitation_current = 0x0b;
- break;
- default:
- dev_err(&st->spi->dev,
- "Invalid value for excitation current(%u)",
- excitation_current);
- ret = -EINVAL;
- goto fail;
- }
- }
- fwnode_handle_put(ref);
- return &thermistor->sensor;
- fail:
- fwnode_handle_put(ref);
- return ERR_PTR(ret);
- }
- static struct ltc2983_sensor *
- ltc2983_diode_new(const struct fwnode_handle *child, const struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor)
- {
- struct ltc2983_diode *diode;
- u32 temp = 0, excitation_current = 0;
- int ret;
- diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL);
- if (!diode)
- return ERR_PTR(-ENOMEM);
- if (fwnode_property_read_bool(child, "adi,single-ended"))
- diode->sensor_config = LTC2983_DIODE_SGL(1);
- if (fwnode_property_read_bool(child, "adi,three-conversion-cycles"))
- diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1);
- if (fwnode_property_read_bool(child, "adi,average-on"))
- diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1);
- /* validate channel index */
- if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) &&
- sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
- dev_err(&st->spi->dev,
- "Invalid chann:%d for differential thermistor",
- sensor->chan);
- return ERR_PTR(-EINVAL);
- }
- /* set common parameters */
- diode->sensor.fault_handler = ltc2983_common_fault_handler;
- diode->sensor.assign_chan = ltc2983_diode_assign_chan;
- ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
- &excitation_current);
- if (!ret) {
- switch (excitation_current) {
- case 10:
- diode->excitation_current = 0x00;
- break;
- case 20:
- diode->excitation_current = 0x01;
- break;
- case 40:
- diode->excitation_current = 0x02;
- break;
- case 80:
- diode->excitation_current = 0x03;
- break;
- default:
- dev_err(&st->spi->dev,
- "Invalid value for excitation current(%u)",
- excitation_current);
- return ERR_PTR(-EINVAL);
- }
- }
- fwnode_property_read_u32(child, "adi,ideal-factor-value", &temp);
- /* 2^20 resolution */
- diode->ideal_factor_value = __convert_to_raw(temp, 1048576);
- return &diode->sensor;
- }
- static struct ltc2983_sensor *ltc2983_r_sense_new(struct fwnode_handle *child,
- struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor)
- {
- struct ltc2983_rsense *rsense;
- int ret;
- u32 temp;
- rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL);
- if (!rsense)
- return ERR_PTR(-ENOMEM);
- /* validate channel index */
- if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
- dev_err(&st->spi->dev, "Invalid chann:%d for r_sense",
- sensor->chan);
- return ERR_PTR(-EINVAL);
- }
- ret = fwnode_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp);
- if (ret) {
- dev_err(&st->spi->dev, "Property adi,rsense-val-milli-ohms missing\n");
- return ERR_PTR(-EINVAL);
- }
- /*
- * Times 1000 because we have milli-ohms and __convert_to_raw
- * expects scales of 1000000 which are used for all other
- * properties.
- * 2^10 resolution
- */
- rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024);
- /* set common parameters */
- rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan;
- return &rsense->sensor;
- }
- static struct ltc2983_sensor *ltc2983_adc_new(struct fwnode_handle *child,
- struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor)
- {
- struct ltc2983_adc *adc;
- adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL);
- if (!adc)
- return ERR_PTR(-ENOMEM);
- if (fwnode_property_read_bool(child, "adi,single-ended"))
- adc->single_ended = true;
- if (!adc->single_ended &&
- sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
- dev_err(&st->spi->dev, "Invalid chan:%d for differential adc\n",
- sensor->chan);
- return ERR_PTR(-EINVAL);
- }
- /* set common parameters */
- adc->sensor.assign_chan = ltc2983_adc_assign_chan;
- adc->sensor.fault_handler = ltc2983_common_fault_handler;
- return &adc->sensor;
- }
- static int ltc2983_chan_read(struct ltc2983_data *st,
- const struct ltc2983_sensor *sensor, int *val)
- {
- u32 start_conversion = 0;
- int ret;
- unsigned long time;
- start_conversion = LTC2983_STATUS_START(true);
- start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan);
- dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n",
- sensor->chan, start_conversion);
- /* start conversion */
- ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion);
- if (ret)
- return ret;
- reinit_completion(&st->completion);
- /*
- * wait for conversion to complete.
- * 300 ms should be more than enough to complete the conversion.
- * Depending on the sensor configuration, there are 2/3 conversions
- * cycles of 82ms.
- */
- time = wait_for_completion_timeout(&st->completion,
- msecs_to_jiffies(300));
- if (!time) {
- dev_warn(&st->spi->dev, "Conversion timed out\n");
- return -ETIMEDOUT;
- }
- /* read the converted data */
- ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan),
- &st->temp, sizeof(st->temp));
- if (ret)
- return ret;
- *val = __be32_to_cpu(st->temp);
- if (!(LTC2983_RES_VALID_MASK & *val)) {
- dev_err(&st->spi->dev, "Invalid conversion detected\n");
- return -EIO;
- }
- ret = sensor->fault_handler(st, *val);
- if (ret)
- return ret;
- *val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT);
- return 0;
- }
- static int ltc2983_read_raw(struct iio_dev *indio_dev,
- struct iio_chan_spec const *chan,
- int *val, int *val2, long mask)
- {
- struct ltc2983_data *st = iio_priv(indio_dev);
- int ret;
- /* sanity check */
- if (chan->address >= st->num_channels) {
- dev_err(&st->spi->dev, "Invalid chan address:%ld",
- chan->address);
- return -EINVAL;
- }
- switch (mask) {
- case IIO_CHAN_INFO_RAW:
- mutex_lock(&st->lock);
- ret = ltc2983_chan_read(st, st->sensors[chan->address], val);
- mutex_unlock(&st->lock);
- return ret ?: IIO_VAL_INT;
- case IIO_CHAN_INFO_SCALE:
- switch (chan->type) {
- case IIO_TEMP:
- /* value in milli degrees */
- *val = 1000;
- /* 2^10 */
- *val2 = 1024;
- return IIO_VAL_FRACTIONAL;
- case IIO_VOLTAGE:
- /* value in millivolt */
- *val = 1000;
- /* 2^21 */
- *val2 = 2097152;
- return IIO_VAL_FRACTIONAL;
- default:
- return -EINVAL;
- }
- }
- return -EINVAL;
- }
- static int ltc2983_reg_access(struct iio_dev *indio_dev,
- unsigned int reg,
- unsigned int writeval,
- unsigned int *readval)
- {
- struct ltc2983_data *st = iio_priv(indio_dev);
- if (readval)
- return regmap_read(st->regmap, reg, readval);
- else
- return regmap_write(st->regmap, reg, writeval);
- }
- static irqreturn_t ltc2983_irq_handler(int irq, void *data)
- {
- struct ltc2983_data *st = data;
- complete(&st->completion);
- return IRQ_HANDLED;
- }
- #define LTC2983_CHAN(__type, index, __address) ({ \
- struct iio_chan_spec __chan = { \
- .type = __type, \
- .indexed = 1, \
- .channel = index, \
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
- .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
- .address = __address, \
- }; \
- __chan; \
- })
- static int ltc2983_parse_dt(struct ltc2983_data *st)
- {
- struct device *dev = &st->spi->dev;
- struct fwnode_handle *child;
- int ret = 0, chan = 0, channel_avail_mask = 0;
- device_property_read_u32(dev, "adi,mux-delay-config-us", &st->mux_delay_config);
- device_property_read_u32(dev, "adi,filter-notch-freq", &st->filter_notch_freq);
- st->num_channels = device_get_child_node_count(dev);
- if (!st->num_channels) {
- dev_err(&st->spi->dev, "At least one channel must be given!");
- return -EINVAL;
- }
- st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors),
- GFP_KERNEL);
- if (!st->sensors)
- return -ENOMEM;
- st->iio_channels = st->num_channels;
- device_for_each_child_node(dev, child) {
- struct ltc2983_sensor sensor;
- ret = fwnode_property_read_u32(child, "reg", &sensor.chan);
- if (ret) {
- dev_err(dev, "reg property must given for child nodes\n");
- goto put_child;
- }
- /* check if we have a valid channel */
- if (sensor.chan < LTC2983_MIN_CHANNELS_NR ||
- sensor.chan > LTC2983_MAX_CHANNELS_NR) {
- ret = -EINVAL;
- dev_err(dev, "chan:%d must be from %u to %u\n", sensor.chan,
- LTC2983_MIN_CHANNELS_NR, LTC2983_MAX_CHANNELS_NR);
- goto put_child;
- } else if (channel_avail_mask & BIT(sensor.chan)) {
- ret = -EINVAL;
- dev_err(dev, "chan:%d already in use\n", sensor.chan);
- goto put_child;
- }
- ret = fwnode_property_read_u32(child, "adi,sensor-type", &sensor.type);
- if (ret) {
- dev_err(dev,
- "adi,sensor-type property must given for child nodes\n");
- goto put_child;
- }
- dev_dbg(dev, "Create new sensor, type %u, chann %u",
- sensor.type,
- sensor.chan);
- if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE &&
- sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
- st->sensors[chan] = ltc2983_thermocouple_new(child, st,
- &sensor);
- } else if (sensor.type >= LTC2983_SENSOR_RTD &&
- sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) {
- st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor);
- } else if (sensor.type >= LTC2983_SENSOR_THERMISTOR &&
- sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) {
- st->sensors[chan] = ltc2983_thermistor_new(child, st,
- &sensor);
- } else if (sensor.type == LTC2983_SENSOR_DIODE) {
- st->sensors[chan] = ltc2983_diode_new(child, st,
- &sensor);
- } else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) {
- st->sensors[chan] = ltc2983_r_sense_new(child, st,
- &sensor);
- /* don't add rsense to iio */
- st->iio_channels--;
- } else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) {
- st->sensors[chan] = ltc2983_adc_new(child, st, &sensor);
- } else {
- dev_err(dev, "Unknown sensor type %d\n", sensor.type);
- ret = -EINVAL;
- goto put_child;
- }
- if (IS_ERR(st->sensors[chan])) {
- dev_err(dev, "Failed to create sensor %ld",
- PTR_ERR(st->sensors[chan]));
- ret = PTR_ERR(st->sensors[chan]);
- goto put_child;
- }
- /* set generic sensor parameters */
- st->sensors[chan]->chan = sensor.chan;
- st->sensors[chan]->type = sensor.type;
- channel_avail_mask |= BIT(sensor.chan);
- chan++;
- }
- return 0;
- put_child:
- fwnode_handle_put(child);
- return ret;
- }
- static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
- {
- u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status;
- int ret;
- /* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */
- ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status,
- LTC2983_STATUS_UP(status) == 1, 25000,
- 25000 * 10);
- if (ret) {
- dev_err(&st->spi->dev, "Device startup timed out\n");
- return ret;
- }
- ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG,
- LTC2983_NOTCH_FREQ_MASK,
- LTC2983_NOTCH_FREQ(st->filter_notch_freq));
- if (ret)
- return ret;
- ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG,
- st->mux_delay_config);
- if (ret)
- return ret;
- for (chan = 0; chan < st->num_channels; chan++) {
- u32 chan_type = 0, *iio_chan;
- ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]);
- if (ret)
- return ret;
- /*
- * The assign_iio flag is necessary for when the device is
- * coming out of sleep. In that case, we just need to
- * re-configure the device channels.
- * We also don't assign iio channels for rsense.
- */
- if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR ||
- !assign_iio)
- continue;
- /* assign iio channel */
- if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) {
- chan_type = IIO_TEMP;
- iio_chan = &iio_chan_t;
- } else {
- chan_type = IIO_VOLTAGE;
- iio_chan = &iio_chan_v;
- }
- /*
- * add chan as the iio .address so that, we can directly
- * reference the sensor given the iio_chan_spec
- */
- st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++,
- chan);
- }
- return 0;
- }
- static const struct regmap_range ltc2983_reg_ranges[] = {
- regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG),
- regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG),
- regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG),
- regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG,
- LTC2983_MULT_CHANNEL_END_REG),
- regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG),
- regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG,
- LTC2983_CHAN_ASSIGN_END_REG),
- regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG,
- LTC2983_CUST_SENS_TBL_END_REG),
- };
- static const struct regmap_access_table ltc2983_reg_table = {
- .yes_ranges = ltc2983_reg_ranges,
- .n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges),
- };
- /*
- * The reg_bits are actually 12 but the device needs the first *complete*
- * byte for the command (R/W).
- */
- static const struct regmap_config ltc2983_regmap_config = {
- .reg_bits = 24,
- .val_bits = 8,
- .wr_table = <c2983_reg_table,
- .rd_table = <c2983_reg_table,
- .read_flag_mask = GENMASK(1, 0),
- .write_flag_mask = BIT(1),
- };
- static const struct iio_info ltc2983_iio_info = {
- .read_raw = ltc2983_read_raw,
- .debugfs_reg_access = ltc2983_reg_access,
- };
- static int ltc2983_probe(struct spi_device *spi)
- {
- struct ltc2983_data *st;
- struct iio_dev *indio_dev;
- struct gpio_desc *gpio;
- const char *name = spi_get_device_id(spi)->name;
- int ret;
- indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
- if (!indio_dev)
- return -ENOMEM;
- st = iio_priv(indio_dev);
- st->regmap = devm_regmap_init_spi(spi, <c2983_regmap_config);
- if (IS_ERR(st->regmap)) {
- dev_err(&spi->dev, "Failed to initialize regmap\n");
- return PTR_ERR(st->regmap);
- }
- mutex_init(&st->lock);
- init_completion(&st->completion);
- st->spi = spi;
- spi_set_drvdata(spi, st);
- ret = ltc2983_parse_dt(st);
- if (ret)
- return ret;
- gpio = devm_gpiod_get_optional(&st->spi->dev, "reset", GPIOD_OUT_HIGH);
- if (IS_ERR(gpio))
- return PTR_ERR(gpio);
- if (gpio) {
- /* bring the device out of reset */
- usleep_range(1000, 1200);
- gpiod_set_value_cansleep(gpio, 0);
- }
- st->iio_chan = devm_kzalloc(&spi->dev,
- st->iio_channels * sizeof(*st->iio_chan),
- GFP_KERNEL);
- if (!st->iio_chan)
- return -ENOMEM;
- ret = ltc2983_setup(st, true);
- if (ret)
- return ret;
- ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler,
- IRQF_TRIGGER_RISING, name, st);
- if (ret) {
- dev_err(&spi->dev, "failed to request an irq, %d", ret);
- return ret;
- }
- indio_dev->name = name;
- indio_dev->num_channels = st->iio_channels;
- indio_dev->channels = st->iio_chan;
- indio_dev->modes = INDIO_DIRECT_MODE;
- indio_dev->info = <c2983_iio_info;
- return devm_iio_device_register(&spi->dev, indio_dev);
- }
- static int ltc2983_resume(struct device *dev)
- {
- struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
- int dummy;
- /* dummy read to bring the device out of sleep */
- regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy);
- /* we need to re-assign the channels */
- return ltc2983_setup(st, false);
- }
- static int ltc2983_suspend(struct device *dev)
- {
- struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
- return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP);
- }
- static DEFINE_SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend,
- ltc2983_resume);
- static const struct spi_device_id ltc2983_id_table[] = {
- { "ltc2983" },
- {},
- };
- MODULE_DEVICE_TABLE(spi, ltc2983_id_table);
- static const struct of_device_id ltc2983_of_match[] = {
- { .compatible = "adi,ltc2983" },
- {},
- };
- MODULE_DEVICE_TABLE(of, ltc2983_of_match);
- static struct spi_driver ltc2983_driver = {
- .driver = {
- .name = "ltc2983",
- .of_match_table = ltc2983_of_match,
- .pm = pm_sleep_ptr(<c2983_pm_ops),
- },
- .probe = ltc2983_probe,
- .id_table = ltc2983_id_table,
- };
- module_spi_driver(ltc2983_driver);
- MODULE_AUTHOR("Nuno Sa <[email protected]>");
- MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
- MODULE_LICENSE("GPL");
|