ltc2983.c 44 KB

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